.index()
In this chapter you will learn:
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><!-- j ava2 s . c om-->
<html>
<head>
<script src="http://java2s.com/style/jquery-1.8.0.min.js">
</script>
<script>
$(function(){
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>
Next chapter...
What you will learn in the next chapter: