Element NodeList length Property - Javascript DOM

Javascript examples for DOM:Element NodeList

Description

The length property returns the number of nodes in a NodeList object.

This property is read-only.

Return Value

A Number, representing the number of nodes in the nodelist

The following code shows how to check how many <p> elements there are in the document:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<body>

<p>The first p element in the document.</p>
<p>Another p element.</p>

<button onclick="myFunction()">Test</button>

<p id="demo">I am also a p element.</p>

<script>
function myFunction() {/*from  ww w.  ja  v  a  2s. co m*/
    var nodelist = document.getElementsByTagName("P").length;
    document.getElementById("demo").innerHTML = nodelist;
}
</script>

</body>
</html>

Related Tutorials