DOM Methods
HTMLCollection.item()
Returns an element at a zero-based position or null if absent. Bracket access instead produces undefined for an absent index. Useful when explicitly handling a missing position with a null check. A named lookup instead uses namedItem; the collection may change before the next lookup because it is live. Example result: A.
Formal Syntax & Structure
html syntaxhtml
list.item(index)Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| index | number | Zero-based position. |
Return Value
Element | null
Element at the requested index, or null if there is none.
Working Example
Example Snippethtml
<div id="items"> <span id="first">A</span> <span>B</span></div><p> Result: <output id="result"></output></p><script> const result = document.getElementById("result"); (async () => { const items = document.getElementById("items"); const list = items.children; result.textContent = list.item(0).textContent; })().catch(error => { result.textContent = "Error: " + error.name; });</script>