The item()
method returns the node at the specified index in a node lists.
item |
Yes | Yes | Yes | Yes | Yes |
nodelist.item(index)
Parameter | Type | Description |
---|---|---|
index | Number | Required. The index of the node to return in the node list |
It returns the Node object at the specified index.
The following code shows how to get the first child node of an element.
<!DOCTYPE html>
<html>
<body><p id="demo">test</p>
<button onclick="myFunction()">test</button>
<script>
function myFunction()<!--from w ww . j a v a 2 s . co m-->
{
var x=document.getElementById("demo");
x.innerHTML=document.body.childNodes.item(0).nodeName;
}
</script>
</body>
</html>
The code above is rendered as follows: