Element getElementsByTagName() Method - Find out how many <p> elements there are inside a <div> element - Javascript DOM

Javascript examples for DOM:Element getElementsByTagName

Description

Element getElementsByTagName() Method - Find out how many <p> elements there are inside a <div> element

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<head>
<style>
div {//  w  w w  .  j a v  a2 s  . co  m
    border: 1px solid black;
    margin: 5px;
}
</style>
</head>
<body>

<div id="myDIV">
  <p>A p element in div.</p>
  <p>Another p element in div.</p>
  <p>A third p element in div.</p>
</div>

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

<p id="demo"></p>

<script>
function myFunction() {
    var x = document.getElementById("myDIV").getElementsByTagName("P");
    document.getElementById("demo").innerHTML = x.length;
}
</script>

</body>
</html>

Related Tutorials