Geert-Jan
Geert-Jan

Reputation: 18925

does calling $el.remove() remove eventhandlers on nested dom-elements as well?

Just a a double-check: does calling $el.remove() remove eventhandlers on nested dom-elements as well (in addition to deleting eventhandlers bound to the element itself)?

Upvotes: 1

Views: 27

Answers (1)

Matt Burland
Matt Burland

Reputation: 45155

From the docs

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.

So I would say yes.

Here's a simple test:

<div id="outer">
    <div id="inner">
        <div id="target">Click me!</div>
    </div>
</div>

$("#target").click(function() {
    alert("clicked");
});

var target = $("#target");
$("#inner").remove();
$("#outer").append(target);

http://jsfiddle.net/Z4BCP/

So I attach a handler to a nested div, then I delete it's parent and append it back to it's grandparent. The click handler is no longer bound.

Compare with: http://jsfiddle.net/Z4BCP/1/

Which uses .detach() instead of .remove()

Upvotes: 3

Related Questions