Hello!

There is a task to remove items and create others. Both to those that need to be deleted, and the new elements have associated .click () and .hover () events.

Should I delete these event handlers (the same .unbind ('click') for example) before deleting old elements and will they sit in the browser’s memory and thus interfere or should I just use .remove () and everything else will be removed?

PS - "on a possible duplicate" The answer to my question completely satisfied me, and there was no such thing in the answer of a possible duplicate, so no this is not a duplicate, but only similar and nothing more. I clearly know which answer was better for me. Thank.

  • not necessary. - Grundy
  • That is, all the attached events will be deleted with it? - Dr. Mc My
  • one
    if the element to which the events are attached is removed, the event handlers will also be deleted if they do not participate in closures and there are no references to them from anywhere. Already had a similar question - Grundy
  • one
  • one
    in this case, there is not much difference - Grundy

1 answer 1

If you use jQuery, then you need to remember that the .remove() method deletes the element itself, everything inside it, all data that is associated with this element, as well as all event handlers ( click , hover , ...)

If you need to remove an element from the page without losing all of the above, then you should use the .detach() method. This may be necessary, for example, if it is necessary to move the element to another place in the document.

If you want to copy an existing element, as well as all its data and behavior, use the .clone(true, true) method .clone(true, true) , where the first true means copying data and handlers, the second true means copying data and handlers from nested elements.

  • one
    thanks, this is what you need =) - Dr. Mc My