HTMLCollection item()
Example
Get the HTML content of the first <p> element:
let text = collection.innerHTML;
This shorthand produces the same result:
let text = collection.innerHTML;
Change the HTML content of the first <p> element:
More examples below.
Description
The item()
method returns the element at a specified index in an
HTMLCollection.
HTMLCollection
An HTMLCollection is an array-like collection (list) of HTML elements.
The length Property returns the number of elements in the collection.
The elements can be accessed by index (starts at 0).
An HTMLCollection is live. It is automatically updated when the document is changed.
See Also
Syntax
Parameters
The index of the element to return.
The index starts at 0.
Return Value
More Examples
Example
Loop over all elements with class="myclass", and change their font size:
for (let i = 0; i < collection.length; i++) {
collection.item(i).style.fontSize ="24px";
}
Example
Get the content of the second <p> element inside "myDIV":
const collection = div.getElementsByTagName("p");
let text = collection[1].innerHTML;
Browser Support
HTMLCollection.item()
is supported in all browsers:
Chrome | Edge | Firefox | Safari | Opera | IE |
Yes | Yes | Yes | Yes | Yes | Yes |