user1432124
user1432124

Reputation:

Function written in jquery is not working properly

I have a page

<div id="aar">  
    <div id="stack0" class="stack">stack1</div>
    <div id="stack1" class="stack">stack1</div>
    <div id="stack2" class="stack">stack1</div>
</div>

in which the following script run

$(document).ready(function(){
    $(".stack").click(function(){
       $("#aar").html($("#aar").html()+'ann');
    });
});

The .stack click function run only one time.Why?

Upvotes: -1

Views: 60

Answers (2)

xdazz
xdazz

Reputation: 160943

This is due to you replaced the old dom with the new one, and the new one don't have bounded click event handler.

And you could just append the new content by using .append method.

$(document).ready(function(){
    $(".stack").click(function(){
        $("#aar").append(this.id);
    });
});

Upvotes: 3

Jay
Jay

Reputation: 1404

User live instead of click:

$(document).ready(function(){
$(".stack").live('click', function(){
    $("#aar").html($("#aar").html()+$(this).attr("id"));
});

});​

Upvotes: 0

Related Questions