Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery unbinding is it necessary? (replacing elements)

I have some elements with events bound. Before I remove those elements, do I need to unbind them first? If I don't will they cause problems?

Thanks.

Edit: I realized I phrased the question incorrectly, so now I'm making an edit, excuse me for that, I but I think it makes more sense to do this than make a new question.

I would have to use unbind or remove on an element if I replace it, right? With the js replacewith method or html method in jquery.

like image 752
Jourkey Avatar asked Sep 06 '09 22:09

Jourkey


People also ask

What is unbind event in jQuery?

jQuery unbind() Method The unbind() method removes event handlers from selected elements. This method can remove all or selected event handlers, or stop specified functions from running when the event occurs. This method can also unbind event handlers using an event object.

What is bind and unbind in jQuery?

jQuery bind() function is used to attach an event handler to elements, while the unbind() is used to detached an existing event handler from elements.

Which method is used to remove a event bind?

The unbind() Method is an inbuilt method in jQuery which is used to remove any selected event handlers. This method can be used to remove particular event handler, or stop specific functions. It works on any event handler using an event object.

Which method is used to cancel outgoing network request are remove all event listeners associated with the component?

componentWillUnmount() Here you can cancel any outgoing network requests, or remove all event listeners associated with the component.


2 Answers

No, you don't need to do that (event unbinding is done automatically at removal).

like image 120
Eran Betzalel Avatar answered Oct 05 '22 03:10

Eran Betzalel


The only time that you would need to explicitly unbind would be if you no longer wanted an event handler for an event on the page.

Removing an element from the DOM causes the events and data for that element to be removed too. Here is the relevant source code

remove: function( selector ) {
    if ( !selector || jQuery.filter( selector, [ this ] ).length ) {
        // Prevent memory leaks
        jQuery( "*", this ).add([this]).each(function(){
            jQuery.event.remove(this);
            jQuery.removeData(this);
        });
        if (this.parentNode)
            this.parentNode.removeChild( this );
    }
} 
like image 32
Russ Cam Avatar answered Oct 05 '22 03:10

Russ Cam