DOM Methods
NodeList.item()
Returns the node at a zero-based index, or null when absent. A NodeList may contain non-element nodes. Guard against null before accessing textContent or other node properties. For the static list from querySelectorAll, newly appended matches are not included. Example result: B.
Formal Syntax & Structure
html syntaxhtml
list.item(index)Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| index | number | Zero-based node index. |
Return Value
Node | null
Node at the requested index, or null if absent.
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.querySelectorAll("span"); result.textContent = list.item(1).textContent; })().catch(error => { result.textContent = "Error: " + error.name; });</script>