.index()

In this chapter you will learn:

  1. Syntax and Description for .index() method
  2. How to get index for list item

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>

Click to view the demo

Next chapter...

What you will learn in the next chapter:

  1. Syntax and Description for .innerHeight()
  2. How to get inner height for a div element