Met Document Getelementsbyclassname
# HTML DOM getElementsByClassName() Method
[ Document Object](#)
## Example
Get all elements with the specified class name:
var x = document.getElementsByClassName("example");
[Try it Β»](#)
* * *
## Definition and Usage
The getElementsByClassName() method returns a collection of all elements in the document with the specified class name, as a NodeList object.
The NodeList object represents an ordered list of nodes. The NodeList object, we can access the 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
document.getElementsByClassName(_classname_)
## Parameters
| Parameter | Type | Description |
| --- | --- | --- |
| _classname_ | String | Required. The class name of the elements you want to get. Multiple class names are separated by spaces, like "test demo". |
## Technical Details
| DOM Version: | Core Level 1 Document Object |
| --- |
| Return Value: | A NodeList object, representing a collection of elements with the specified class name. The elements in the collection are ordered by their appearance in the code. |
* * *

## More Examples
## Example
Get all elements with class names "example" and "color", and change their color:
var x = document.getElementsByClassName("example color");
[Try it Β»](#)
## Example
Find out how many elements in the document have the class name "example" (using the length property of the NodeList object):
var x = document.getElementsByClassName("example").length;
[Try it Β»](#)
## Example
Change the background color of all elements with class name "example":
var x = document.getElementsByClassName("example");
var i;
for (i = 0; i < x.length; i++) {
x.style.backgroundColor = "red";
}
[Try it Β»](#)
* * *
## Related Pages
CSS Tutorial: (#)
CSS Reference: [CSS _.class_ Selector](#)
HTML DOM Reference: [_element_.className](#)
YouTip