Henson
Henson

Reputation: 5933

Bugs with jquery.live()?

Can anyone explain to me what's going on?

http://jsfiddle.net/K5yvp/2/

So the way to "achieve" the bugs is :

I know the codes I wrote in fiddle is no good, since it's just a simplified version of the problem I found out yesterday. So basically what I meant to do is :

I've tried some ways to solve this but it's still quite bugging me that my codes are generating bugs(?) . Some of them are :

Hope this is clear enough. Thanks!

Upvotes: 0

Views: 50

Answers (2)

griegs
griegs

Reputation: 22770

var title
$('.show').live('click',function(){
     title = $(this).attr('title');
    $('<p class="next" />').text('next').appendTo($('.parent'));
    $('<p class="hide" />').text('hide').appendTo($('.parent'));
    $('.parent').fadeIn(500,function(){

});
});    

$('.hide').live('click',function(){
     $('.parent').empty();
});

$('.next').live('click',function(){
        alert('aaa '+title);
        return false;
    });

Upvotes: 1

Cory Kendall
Cory Kendall

Reputation: 7324

The error may lie in your understanding of live. Live does not run once, and attach a handler to every element it finds. Rather, it allows you to attach handlers to elements that may not exist yet.

So every time this line is hit in your fiddle: $('.next').live('click',function(){, you're installing another handler to every object with class next that ever exists, even if that object hasn't yet been created.

Upvotes: 2

Related Questions