Document getElementsByClassName() Method - Find out how many elements with class="example" there are in the document - Javascript DOM

Javascript examples for DOM:Document getElementsByClassName

Description

Document getElementsByClassName() Method - Find out how many elements with class="example" there are in the document

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<body>

<div class="example">
A div element with class="example"
</div>/*  w w  w .j a va  2s.  c om*/

<div class="example">
Another div element with class="example"
</div>

<p class="example">A p element with class="example"</p>

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

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

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

</body>
</html>

Related Tutorials