Element item() Method - Javascript DOM

Javascript examples for DOM:Element item

Description

The item() method returns a node by index in a NodeList object.

The nodes are sorted as they appear in the source code, and the index starts at 0.

Parameter Values

Parameter Type Description
index Number Required. The index of the node to return, in the node list. Note: The index starts at 0

Return Value:

A Node object, representing the node at the specified index.

The following code shows how to Get the HTML content of the first <p> element (index 0) inside the document:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<body>

<p>The first p element in the document.</p>
<p>Another p element.</p>

<button onclick="myFunction()">Test</button>

<p id="demo"></p>

<script>
function myFunction() {//from w  w  w .  j a va 2  s. c o  m
    var nodelist = document.getElementsByTagName("P").item(0).innerHTML;
    document.getElementById("demo").innerHTML = nodelist;
}
</script>

</body>
</html>

Related Tutorials