HTML DOM getElementsByClassName() Method Last Updated : 02 Sep, 2024 Comments Improve Suggest changes Like Article Like Report 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 by 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 the information that wants.Example 1: This example describes the getElementsByClassName() method for getting access to an 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() MethodExample 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() MethodSupported Browser: The browsers supported by DOM getElementsByClassName() are listed below: Google ChromeEdge FirefoxOperaSafari Comment More infoAdvertise with us Next Article HTML DOM getElementsByClassName() Method A Archana choudhary Follow Improve Article Tags : JavaScript HTML-DOM HTML-Methods Similar Reads HTML | DOM Style margin Property The DOM Style margin Property is used to sets or returns the margin of an element.We can set the different size of margins for individual sides(top, right, bottom, left).Margin properties can have following values: Length in cm, px, pt, etc.Width % of the element.Margin calculated by the browser: au 2 min read HTML DOM nodeName Property The nodeName property is used to return the name of the specified node as a string. It returns different values for different nodes such as if the node attributes, then the returned string is the attribute name, or if the node is an element, then the returned string is the tag name. It is a read-onl 2 min read HTML DOM textContent Property The textContent property in HTML is used to set or return the text content of the specified node and all its descendants. This property is very similar to nodeValue property but this property returns the text of all child nodes.Syntax: It is used to set the text of node. node.textContent = textIt is 2 min read HTML ondrop Event Attribute The ondrop event attribute is used to drag an element or text and drop it into a valid droppable location or target. The drag and drop is a common feature of HTML 5. There are different events that are used and occur before ondrop event. Events occur on the draggable targetEvents occur on the drop t 3 min read HTML DOM item() Method The item() method is used to return the node at the specified index. The nodes are sorted as they appear in the source code. The index of the node list starts with 0. Syntax:nodelist.item( index )ornodelist[ index ] Parameters: This method accepts single parameter index which is used to hold the ind 2 min read HTML DOM Style borderRight Property The DOM style borderRight property is used to set or return the three different border-right properties such as border-right-width, border-right-style, and border-right-color of an element. Syntax: It returns the borderRight Property.object.style.borderRightIt is used to set the borderRight property 2 min read HTML DOM Style borderTop Property The DOM style borderTop property is used to set or return the three different border-top property such as border-top-width, border-top-style, and border-top-color of an element. Syntax: It returns the borderTop property. object.style.borderTopIt is used to set the borderTop property. object.style.bo 2 min read HTML DOM documentElement Property The DOM documentElement property is used to return the documentElement as an Element Object. It is a read-only property. It returns a <HTML> Element. Syntax:document.documentElement Return Value: It returns the documentElement of the document or an element object. Example: Below program illust 2 min read HTML DOM Style borderLeft Property HTML DOM Style borderLeft property allows manipulation of the left border style of an element via JavaScript. It controls the border's width, style, and color, affecting the element's visual presentation and layout. HTML DOM Style borderLeft Property Syntax:Get the borderLeft Property.object.style.b 2 min read HTML DOM setNamedItem() Method The setNamedItem() method is used to add a particular node to an attribute node using its name. These attribute nodes are collectively called as namedNodeMap. It can be accessed through a name. If a node is already present in the document, it will replace it and return the updated value. The setName 2 min read Like