0

if I add a jQuery click handler to an object, i.e.

$('#my-link').click(function() {
// stuff
});

But then I remove the link from my page, do I have to remove my click handler, and if so, how do I go about that?

4 Answers 4

3

No, you don't need to. From the jQuery documentation:

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.

Sign up to request clarification or add additional context in comments.

Comments

0

Per the jQuery documentation remove() removes all events bound to the element: http://api.jquery.com/remove/

Comments

0

The click handler binds on the element when it gets selected by the selector engine. When you remove the link from the page the action won't be bound so there wont be a problem.

If you remove the link by code you can use the unbind('click') function to remove the actual action.

Comments

0

Nope. Once the element is removed from the DOM, the onclick event is removed with it.

Only if the click event is being delegated through the document element would it need to be removed manually, and jQuery takes care of this for you as long as you are using jQuery's .remove() method.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.