YouTip LogoYouTip

Met Element Getelementsbyclassname

# HTML DOM getElementsByClassName() Method [![Image 4: Element Object Reference Manual](#) Element Object](#) ## Example Modify the text of the first item (index 0) with class="child" in the list with class="example": var list = document.getElementsByClassName("example"); list.getElementsByClassName("child").innerHTML = "Milk"; Before modifying the text: * Coffee * Tea After modifying the text: * Milk * Tea [Try it Β»](#) * * * ## Definition and Usage The getElementsByClassName() method returns a collection of all elements in the document with the specified class names, as a NodeList object. The NodeList object represents an ordered list of nodes. The NodeList object allows us to access nodes in the list by their index number (starting from 0). **Tip:** You can use the (#) property of the NodeList object to determine the number of elements with the specified class name, and loop through all elements to get the one you need. * * * ## Browser Support The numbers in the table specify the first browser version that fully supports the method. | Method | | | | | | | --- | --- | --- | --- | --- | --- | | getElementsByClassName() | 4.0 | 9.0 | 3.0 | 3.1 | 9.5 | * * * ## Syntax _element_.getElementsByClassName(_classname_) ## Parameter Values | Parameter | Type | Description | | --- | --- | --- | | _classname_ | String | Required. The class name of the elements you want to find. Multiple class names are separated by spaces, like "test demo". | ## Technical Details | DOM Version: | Core Level 1 Element Object | | --- | | Return Value: | A NodeList object representing a collection of elements with the specified class names. The elements in the collection are ordered by their appearance in the code. | * * * ![Image 5: Examples](#) ## More Examples ## Example Change the background color of the second element with class="child" inside the
element: var x = document.getElementById("myDIV"); x.getElementsByClassName("child").style.backgroundColor = "red"; [Try it Β»](#) ## Example Find out how many elements with class="child" are inside the
element (using the length property of the NodeList): var x = document.getElementById("myDIV").getElementsByClassName("child").length; The output of _x_ will be: 3 [Try it Β»](#) ## Example Change the background color of the first element with both class names "child" and "color" inside the element with class="example": var x = document.getElementsByClassName("example"); x.getElementsByClassName("child color"
← Cpp Classes ObjectsThread Status β†’

YouTip © 2024-2026 | Home | Learn Technology, Build Dreams!

All content is for educational and learning purposes only.