Javascript Reference - HTML DOM item() Method








The item() method returns the node at the specified index in a node lists.

Browser Support

item Yes Yes Yes Yes Yes

Syntax

nodelist.item(index)

Parameters

Parameter Type Description
index Number Required. The index of the node to return in the node list




Return Value

It returns the Node object at the specified index.

Example

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: