Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

HTML DOM getElementsByClassName() Method

  • Last Updated : 12 Nov, 2021

The getElementsByClassName() method in Javascript 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. The index value will start with 0. 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 is a required method that takes only one parameter, which is a string containing space-separated class names of the elements that are to be searched for. For searching with multiple class names, it must be separated with space.

Note: We can use the length property that returns the collection of all HTML elements in a document for the specified class name & then by looping through the HTML elements, we can take information that wants.

Example 1: This example describes the getElementsByClassName() method for getting access to HTML element by its class name.

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:

document.getElementsByClassName() Method

Example 2:  This example describes the use of the document.getElementsByClassName() method that accesses all the 3 button classes with the specific color & alters the color of the button on clicked & the last button resets all the above 3 buttons to their initial state.

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:

document.getElementsByClassName() Method

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

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

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!