1

As piece of code is better than thousand word

// this is our dynamic created element.,
var $test = $('<button>If you add this event is working, if you remove this, and add again, event is not working...</button>');

// this is our event
$test.click(function(){
    alert('Fooobar'); // fires only first time
});
// $test.on('click',function(){ <-- same behaviour

$('#add').click(function(){
    $('#container').append( $test );
});
$('#remove').click(function(){
    $('#container').html(''); // This is destroying all $test events!!!
});

How can i remove element, append it again and save events?

JS Fiddle:

I would like to remove element without destroying events.

3 Answers 3

3

I think you are looking for jQuery's detach method.

The .detach() method is the same as .remove(), except that .detach() keeps all jQuery data associated with the removed elements. This method is useful when removed elements are to be reinserted into the DOM at a later time.

You could detach $test before the $('#container').html(''); code so $test could be reinserted with all events still attached.

Sign up to request clarification or add additional context in comments.

Comments

1
var $test = $('<button>If you add this event is working, if you remove this, and add again, event is not working...</button>');
              
// here you've to use delegate event
// using jQuery on() method

$('#container').on('click', $test, function(){
    alert('Fooobar');
});

$('#add').click(function(){
    $('#container').append( $test );
});
$('#remove').click(function(){
    $('#container').html('');
});

Demo

For delegate event (aka live event) handling you need to use on() like:

$(Parent).on(eventName, target, handler);

Here Parent is a static-element that is a container to target and target is the element to which the event will bind.

Read more about jQuery on() method.


You can also do following

var $test = $('<button>If you add this event is working, if you remove this, and add again, event is not working...</button>');

$('#add').click(function() {
    $('#container').append($test.on('click', function() {
        alert('Fooobar');
    }));
});
$('#remove').click(function() {
    $('#container').html('');
});

Demo

2 Comments

That's it! But what if I need to put $test somewhere else than #conatiner?
@EXQStudio as you $test contains only <button> so it firing the click event to all buttons. you can use any class name to distinguish the dynamically added button.
-2

Bind the event's function to a parent element.

$('body').on('click', 'button', function(){
    alert('Delegated Click function');
});

Replace button with your selector.

2 Comments

I know this trick, but this doesn't work for me. Thanks anyway. I would like to remove element without destroying events.
@EXQ Studio -- There's no way to remove an element from the DOM (cleanly) and save it's events. This is why we delegate functionality. Choosing to go against the grain will cause unnecessary overhead, and also brings challenges (such as this) to the plate. I don't think you're going to find repreive.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.