Get elements by its class names in JavaScript

Description

The following code shows how to get elements by its class names.

Example


<!--from w w  w .j  a  v  a2s .c o m-->
<!DOCTYPE HTML>
<html>
<body>
<p class="myClass">myClass </p>
<p class="myClass anotherClass">two classes </p>
<script>
var elems = document.getElementsByClassName("myClass");
document.writeln(elems.length );
</script>
</body>
</html>

Click to view the demo

The code above generates the following result.

Get elements by its class names in JavaScript