I am stuck with a problem here, please help. What my requirement is to get the index value of the div clicked. Have 2 lists, and i need to call a function doSomething(); only when the user clicks the first div set. Is there anyway that i can achieve it.
The scripts and HTML is as follows
$(document).ready(function() {
$(".list-wrapper li").click(function() {
var index = $(".list-wrapper div").index(this);
alert(index);
});
});
<div class="list-wrapper">
<ul>
<li>List 1</li>
<li>List 2</li>
<li>List 3</li>
</ul>
</div>
<div class="list-wrapper">
<ul>
<li>List 1</li>
<li>List 2</li>
<li>List 3</li>
</ul>
</div>
Please help…
jQuery has powerful selectors, so usually you don’t need to check the index after the event. For example:
For the first div on your page:
For the first
<li>s in every list:If you still want to find the index, you need to search for the parent
divof the clickedli. Also, the selector.list-wrapper divis empty – you don’t have anydivs inlist-wrapper. This will work: