jQuery: Any way to "refresh" event handlers?

Posted by Legend on Stack Overflow See other posts from Stack Overflow or by Legend
Published on 2010-05-25T21:54:28Z Indexed on 2010/05/25 22:01 UTC
Read the original article Hit count: 148

Filed under:
|

I have two divs, one that holds some stuff and the other with all possible stuff. Clicking on one of the divs will transfer items to the other div. The code I came up with is:

$("#holder > *").each(function() {
    $(this).click(function(e) {
        $(this).remove();
        $("#bucket").append(this);
    });
});

$("#bucket > *").each(function() {
    $(this).click(function(e) {
        $(this).remove();
        $("#holder").append(this);
        });
});

This one works perfectly, except that the event handlers need to be refreshed once I append or remove elements. What I mean is, if I first click on an element, it gets added to the other div, but if I click on this element again, nothing happens. I can do this manually but is there a better way to achieve this?

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about jQuery