Skip to content
Related Articles

Related Articles

HTML | DOM getElementsByClassName() Method
  • Last Updated : 10 Dec, 2020

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:  

HTML




<!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: 

HTML




<!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: 

  • Before clicking any button:

 

  • After clicking first three buttons:

Supported Browser: The browsers supported by DOM getElementsByClassName() are listed below: 

  • Google Chrome 4.0
  • Internet Explorer 9.0
  • Firefox 3.0
  • Opera 9.5
  • Safari 3.1

 

My Personal Notes arrow_drop_up
Recommended Articles
Page :