javascript - Getting index of clicked list element -


how can index of clicked element in list?

so if have following list:

  • first
  • second
  • third

when click third, index 2, provided index starts 0.

with code 0s:

html:

<ul>   <li><a href="#" class="listelem">first</a></li>   <li><a href="#" class="listelem">second</a></li>   <li><a href="#" class="listelem">third</a></li> </ul> 

js:

$(".listelem").click(function(param) {   console.log($(this).index()); }); 

here fiddle.

you should use parent() return index of li demo

$(".listelem").click(function(param) {   console.log($(this).parent().index()); }); 

Comments

Popular posts from this blog

magento2 - Magento 2 admin grid add filter to collection -

Android volley - avoid multiple requests of the same kind to the server? -

Combining PHP Registration and Login into one class with multiple functions in one PHP file -