Jquery search item position

<ul>
   <li>one</li>
   <li>element</li>
   <li>text</li>
   <li>val</li>
</ul>

How can I get the position ulpressed li?

+3
source share
3 answers

I think this will do it for you:

$("li").click(function () {

   alert($(this).index());

});

Note that the index () function returns the index of the element in the jquery collection. If there are multiple lists on a page, make sure your selector selects only the list items you want.

+9
source
$('ul li').click(function() {
    alert(   $(this).parent().find('li').index(this)  );
});

Link

I tested only one <ul>. You will need .eachit if you do this on multiple <ul>s.

+2
source

Source: https://habr.com/ru/post/1729119/


All Articles