Code has been added to clipboard!

Understanding jQuery .detach() and How It Differs From remove()

Reading time 1 min
Published Jan 18, 2018
Updated Sep 27, 2019

jQuery detach: Main Tips

  • jQuery .detach() removes selected elements from the DOM.
  • Unlike a similar remove() method, .detach() keeps the element's jQuery data.

Understanding .detach()

jQuery .detach() method removes the selected elements. However, it keeps the events and data, so they can be inserted again at a later time.

In the example below, <div> element is removed after a click event:

Example
$("button").click(() => {
    $("div").detach();
});

The syntax for jQuery .detach() is as follows:

$(selector).detach();

All you need to specify is a selector to filter the elements to remove.

Latest Coupon Found:

Verified STAFF PICK

TOP-RATED CODING COURSES

Learn the skills of tomorrow

The best time to learn programming is now - follow this link to access 100+ coding courses and enjoy learning at a very low cost!

Expiration date: 13/05/2021
2287 People Used
Only 92 Left
Rating
5.0