HTML | DOM getElementsByClassName() Method
The getElementsByClassName() method returns an object containing all the elements with the specified class names in the document as objects.Each element in the returned object can be accessed by its index. This method can be called upon any individual element to search for its descendant elements with the specified class names.
Syntax:
document.getElementsByClassName(classnames)
Parameters:This method takes only one parameter, which is a string containing space-separated class names of the elements to search for.
Example 1:
<!DOCTYPE html> <html> <head> <title>DOM getElementByClassName() Method</title> <style> h1 { color: green; } body { text-align: center; } .example { padding: 10px; margin: auto; margin-top: 10px; border: 1px solid black; width: 300px; } </style> </head> <body> <h1>GeeksforGeeks</h1> <h2>DOM getElementByClassName() Method</h2> <div> <h4 class="example">div1</h4> <h4 class="yellowBorder example">div2</h4> <h4 class="greenBorder example">div3</h4> <h4 class="example">div4</h4> </div> <script> document.getElementsByClassName('greenBorder example')[0] .style.border="10px solid green"; document.getElementsByClassName('yellowBorder example')[0] .style.border="10px solid yellow"; </script> </body> </html> |
Output:

Example 2:
<!DOCTYPE html> <html> <head> <title>DOM getElementByClassName() Method</title> <style> h1 { color: green; } body { text-align: center; } button { background-color: black; color: white; width: 300px; padding: 10px; margin: 10px; cursor: pointer; } </style> </head> <body> <h1>GeeksforGeeks</h1> <h2>DOM getElementByClassName() Method</h2> <div> <button onclick="red()" class="black red"> Click to change to red button </button><br> <button onclick="blue()" class="black blue"> Click to change to blue button </button><br> <button onclick="yellow()" class="black yellow"> Click to change to yellow button </button><br> <button onclick="black()"> Click to change to all buttons to initial state </button> </div> <script> function red() { document.getElementsByClassName('red')[0] .style.backgroundColor='red'; } function blue() { document.getElementsByClassName('blue')[0] .style.backgroundColor='blue'; } function yellow() { document.getElementsByClassName('yellow')[0] .style.backgroundColor='yellow'; } function black() { var elements=document.getElementsByClassName('black'); for (var i = 0; i < elements.length; i++) { elements[i].style.backgroundColor='black'; } } </script> </body> </html> |
Output:


Supported Browser: The browsers supported by DOM getElementsByClassName() are listed below:
- Goole Chrome 4.0
- Internet Explorer 9.0
- Firefox 3.0
- Opera 9.5
- Safari 3.1
Recommended Posts:
- HTML | DOM contains() Method
- HTML | DOM createAttribute() Method
- HTML | DOM getAttributeNode() Method
- HTML | DOM isSameNode() Method
- HTML | DOM hasAttributes() Method
- HTML | DOM Storage key() Method
- HTML | DOM click() Method
- HTML | DOM exitFullscreen() Method
- HTML | DOM cloneNode() Method
- HTML | DOM createElement() Method
- jQuery | html() Method
- HTML | DOM querySelector() Method
- HTML | canvas arc() Method
- HTML | DOM Select add Method
- HTML | DOM item() Method
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : Akanksha_Rai



