jQuery .index() return index value for DOM element
Syntax and Description
.index()
accepts a DOM element, and returns its zero-based index value.
Get list item index
The following code gets an element from list by id then checks its index.
<!DOCTYPE html>
<html>
<head>
<script src="http://java2s.com/style/jquery-1.8.0.min.js">
</script>
<script>
$(function(){<!-- w w w .ja v a2 s .com-->
var item2 = document.getElementById("item2");
document.writeln($("li").index(item2));
});
</script>
</head>
<body>
<ul>
<li id="item0">A</li>
<li id="item1">B</li>
<li id="item2">java 2s.com</li>
<li id="item3">D</li>
<li id="item4">E</li>
<li id="item5">F</li>
</ul>
</body>
</html>