YouTip LogoYouTip

Met Htmlcollection Item

# HTMLCollection item() Method [![Image 3: DOM HTMLCollection Object Reference](#) DOM HTMLCollection](#) * * * ## Definition and Usage The item() method returns the element at the specified index position in an HTMLCollection object. The elements in an HTMLCollection object are ordered the same as they appear in the document source code, with the index starting at 0. You can also use the following shorthand to get the same result: var x = document.getElementsByTagName("P"); ## Syntax HTMLCollection.item(index) or: HTMLCollection | Parameter | Type | Description | | --- | --- | --- | | _index_ | Number | Required. The index value of the element. **Note:** The starting position is 0, the last element index is HTMLCollection.length-1, and the index must be within this range. | * * * ## Browser Support | Method | | | | | | | --- | --- | --- | --- | --- | --- | | item() | Yes | Yes | Yes | Yes | Yes | * * * ## Examples ## Example Change the HTML content of the first p element: document.getElementsByTagName("P").item(0).innerHTML = "Paragraph has been modified"; [Try it Β»](#) ## Example Loop through all elements with class="myclass" and change their background color: var x = document.getElementsByClassName("myclass"); for(i = 0; i<x.length; i++){x.item(i).style.backgroundColor = "red"; } [Try it Β»](#) ## Example Get the HTML content of the first p element and insert it into a div element: var div = document.getElementById("myDIV"); var x = div.getElementsByTagName("P").item(0).innerHTML; [Try it Β»](#) * * * ## Related Articles HTMLCollection: (#) Property HTML Element: [getElementsByClassName()](#) Method HTML Element: [getElementsByTagName()](#) Method * * DOM HTMLCollection](#)
← Ts TutorialPython3 Func Hex β†’