The HTML DOM item() method returns a node from a node list object.
Note − The item() method is used to provide index value in a node list object. It is same as getting node by passing index value in ‘[index]’ on node list object.
Syntax
Following is the syntax −
Calling item(index), where index starts from 0
nodeListObject.item(index)
Example
Let us see an example for item() method −
<!DOCTYPE html>
<html>
<head>
<title>item()</title>
<style>
form {
width:70%;
margin: 0 auto;
text-align: center;
}
* {
padding: 2px;
margin:5px;
}
input[type="button"] {
border-radius: 10px;
}
ul{
width: 30%;
margin: 0 auto;
}
</style>
</head>
<body>
<form>
<fieldset>
<legend>item( )</legend>
<h2>Menu</h2>
<ul>
<li onclick="getOrder(0)">Mac & Cheese</li>
<li onclick="getOrder(1)">Pizza</li>
<li onclick="getOrder(2)">Burger</li>
</ul>
<div id="divDisplay">Click on menu item to order it</div>
</fieldset>
</form>
<script>
var divDisplay = document.getElementById("divDisplay");
var menuSelect = document.getElementsByTagName("li");
function getOrder(index) {
divDisplay.textContent = 'Order: '+(menuSelect.item(index)).textContent;
}
</script>
</body>
</html>Output
This will produce the following output −
Before clicking ‘<li>’ element −

After clicking ‘<li>’ element −
