Skip to content Skip to sidebar Skip to footer

JQuery Remove() Callback?

Is there an official way to hook in to jQuery.remove() so that a function can be called before/after? I have a system whereby certain handlers are attached to elements, and sometim

Solution 1:

you can use jQuery.when():

$.when($('div').remove()).then( console.log('div removed') );

Solution 2:

Use a custom event, attach handlers to the custom event that fire before/after the remove. For example,

$( document ).bind( 'remove', function( event, dom ){

    $( document ).trigger( 'beforeRemove', [ dom ] );
    $( dom ).remove();
    $( document ).trigger( 'afterRemove', [ dom ] );
});

$( document ).trigger( 'remove', 'p' ); //Remove all p's

Solution 3:

Here's a nifty hack - you might wanna give it a try.

$('div').hide(1, function(){
    // callback
    $(this).remove();
});

Post a Comment for "JQuery Remove() Callback?"