jQuery index() Get index relative to sibling elements

Introduction

Click the list items to get the index position, relative to its sibling elements.

View in separate window

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
  $("li").click(function(){
    document.getElementById("demo").innerHTML = $(this).index();
  });/*from ww  w .j  a v a  2 s .c  om*/
});
</script>
</head>
<body>

<p id="demo"></p>
<ul>
  <li>CSS</li>
  <li>HTML</li>
  <li>Javascript</li>
</ul>

</body>
</html>



PreviousNext

Related