getElementsByClassName() method in javascript
getElementsByClassName() method is used to retrieve every element with the given single class value or multiple class names.
Example Explanation
- In the above example we have six div elements (clsDiv1, clsDiv2, clsDiv3, clsDiv4), button and a Javascript function is declared for it (fun_name();).
- When the button is clicked Javascript function is called.
- In the Javascript function, first we are getting the div elements with specified class value(document.getElementById("clsDiv1");) and storing in elem variable.
- In the next step a for loop is created and every element in elem variable is displayed (alert(elem2.outerHTML);).