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:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

  • Before clicking any button:
  • After clicking first three buttons:


  • My Personal Notes arrow_drop_up

    Check out this Author's contributed articles.

    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.




    Article Tags :

    Be the First to upvote.


    Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.