0

I just want a confirmation about what I'm doing in my app.

I created some jquery plugins which add some elements and events in the DOM of my page.

For example, I init the plugin in a DOM element of my page:

$("#myplugin_container").myplugin('init', {'data':mydata});

What I want to know : if I remove the container with remove function

$("#myplugin_container").remove();

, will I have memory leaks on the plugin or will the jquery object be removed correctly ?

I hope my explanations are clear...

Thanks !

2
  • 1
    ... it depends on the code within your plugin (e.g. the scope you place the element and the data you give to it). If you store all the settings and data for the element local to it (e.g via .data()) there's a very strong chance of their been no leaks. However, if you store the element and the data in another scope for whatever reason (e.g. in a persistant array), then the references to it will be held even after you've called .remove(), and so the memory will "leak" (in JavaScript terms).
    – Matt
    Jun 11, 2012 at 13:31
  • In my plugin, I store the data in an array. So, you are certainly right about the lost of the reference to this array. I will use .data(). Thanks
    – bastien
    Jun 11, 2012 at 14:53

1 Answer 1

1

jQuery will remove all event handlers and other data associated with the element when you use .remove(), so there shouldn't be any memory leaks.

From jQuery documentation of remove():

Similar to .empty(), the .remove() method takes elements out of the DOM. Use .remove() when you want to remove the element itself, as well as everything inside it. In addition to the elements themselves, all bound events and jQuery data associated with the elements are removed. To remove the elements without removing data and events, use .detach() instead.

0

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.