Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do I need to detach events in jQuery when I remove elements

I have a UI using dynamic tabs, so content can be loaded into a tab and then the tab can be closed and the content removed from the page.

When I load content into a tab I attach a lot of events to elements using jQuery.

What happens when I remove these elements from the page? Does jQuery need to know?

Also, does it matter if I attach an event multiple times? For example, in my tab load I might attach an event using a class selector like $('.submitButton').click(...). But I might already have other tabs open, which have already had the submitButton event attached. In this case, I'll be re-attaching the same event. Is there any problem with this?

like image 208
fearofawhackplanet Avatar asked Jul 06 '10 12:07

fearofawhackplanet


People also ask

Are event listeners removed when element is removed?

According to the jquery Documentation when using remove() method over an element, all event listeners are removed from memory. This affects the element it selft and all child nodes. If you want to keep the event listners in memory you should use . detach() instead.

Does jQuery remove unbind events?

jQuery unbind() MethodThe 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.

What is difference between detach () and remove () method in jQuery?

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.

What is the use of detach in jQuery?

jQuery detach() Method The detach() method removes the selected elements, including all text and child nodes. However, it keeps data and events. This method also keeps a copy of the removed elements, which allows them to be reinserted at a later time.


2 Answers

If you use jQuery methods .remove() or .empty(), they will clean up all events (and other data) that were assigned with jQuery.

  • http://api.jquery.com/remove/
  • http://api.jquery.com/empty/

From the docs for remove():

In addition to the elements themselves, all bound events and jQuery data associated with the elements are removed.

and for empty():

To avoid memory leaks, jQuery removes other constructs such as data and event handlers from the child elements before removing the elements themselves.

If you used native API methods of removing, all that data will hang around. So better to use jQuery methods.

like image 180
user113716 Avatar answered Oct 17 '22 14:10

user113716


If you remove an element with .remove() all bound events and jQuery data associated with the element is removed.

Other than .detach(), which will remove the element from the DOM, but keeps all associated data and events in memory (which is useful if you want to re-insert that element at a later time)

like image 3
jAndy Avatar answered Oct 17 '22 16:10

jAndy