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:
The syntax for jQuery .detach()
is as follows:
$(selector).detach();
All you need to specify is a selector to filter the elements to remove.