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 0
s:
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()); });
you should use parent()
return index of li
demo
$(".listelem").click(function(param) { console.log($(this).parent().index()); });
Comments
Post a Comment