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

Lists in Python -

android - can not access to progress bar in an other activity -

java - Vaadin 7 Pass Data Between Views -