get the index position of an element, relative to the selector. - Javascript jQuery Method and Property

Javascript examples for jQuery Method and Property:index

Introduction

The element can be specified using a DOM element, or a jQuery selector.

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(){
    $("button").click(function(){
        console.log($(".hot").index($("#myId")));
    });/*  w w w. j  a v  a 2 s .  c  om*/
});
</script>
</head>
<body>

<button>Get index</button>

<ul>
  <li>A</li>
  <li class="hot">B</li>
  <li class="hot" id="myId">C</li>
</ul>

</body>
</html>

Related Tutorials