I'm creating new < li > elements with a live() click function, but jquery won't tell me the index of newly created elements. Here's the code:
$esl = $('.dynamicLink');
$esl.live('click',function(){
var dynamicIndex = $esl.index(this);
alert(dynamicIndex);
});
Whenever I click the dynamicLink, it returns "-1" as the index. Any suggestions?
Because live
uses event delegation to "bind" events to future elements, you need to call index
on an up-to-date list of elements. Try this:
$esl = $('.dynamicLink');
$esl.live('click',function(){
var dynamicIndex = $('.dynamicLink').index(this);
alert(dynamicIndex);
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With