Find out how many P elements in the document:
Click the button to alert the number of P elements on this document:
<!DOCTYPE html> <html> <body> <p>The length property returns the number of elements in a HTMLCollection.</p> <p>The length property returns the number of elements in a HTMLCollection.</p> <p>The length property returns the number of elements in a HTMLCollection.</p> <p>The length property returns the number of elements in a HTMLCollection.</p> <p>The length property returns the number of elements in a HTMLCollection.</p> <button onclick="myFunction()">Count P elements</button> <p id="myP"></p> <script> function myFunction() {/*from w w w . j a v a2 s . c om*/ var l = document.getElementsByTagName("P").length; document.getElementById("myP").innerHTML = l; } </script> </body> </html>
The length property returns the number of elements in a HTMLCollection.
This property is read-only.