index() Method - Javascript jQuery Method and Property

Javascript examples for jQuery Method and Property:index

Description

The index() method returns the index position of specified elements relative to other specified elements.

Syntax

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:

Demo Code

ResultView the demo in separate window

<!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>

Related Tutorials