Javascript examples for jQuery Method and Property:index
The index() method returns the index position of specified elements relative to other specified elements.
Parameter | Require | Description |
---|---|---|
element | Optional. | element to get the index position of. DOM element or a jQuery selector |
The following code shows how to get the index of the clicked <li> element, relative to its siblings:
<!DOCTYPE html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <script> $(document).ready(function(){ $("li").click(function(){ console.log($(this).index()); });//from www . j a va 2 s . c o m }); </script> </head> <body> <p>Click the list items to get the index position</p> <ul> <li>A</li> <li>B</li> <li>C</li> </ul> </body> </html>