Deleting Elements

Along with insertions and replacements, jQuery also defines methods for deleting elements. empty() removes all children (including text nodes) of each of the selected elements without altering the elements themselves. The remove() method, by contrast, removes the selected elements (and all of their content) from the document. remove() is normally invoked with no arguments and removes all elements in the jQuery object. If you pass an argument, however, it is treated as a selector, and only elements of the jQuery object that also match the selector are removed. (If you just want to remove elements from the set of selected elements, without removing them from the document, use the filter() method, which is covered in Selection Methods.) Note that it is not necessary to remove elements before reinserting them into the document: you can simply insert them at a new location and they will be moved.

The remove() method removes any event handlers (see Chapter 4) and other data (see Getting and Setting Element Data) you may have bound to the removed elements. The detach() method works just like remove() but does not remove event handlers and data. detach() may be more useful when you want to temporarily remove elements from the document for later reinsertion.

Finally, the unwrap() method performs element removal in a way that is opposite of the wrap() or wrapAll() method: it removes the parent of each selected element without affecting the selected elements or their siblings. ...

Get jQuery Pocket Reference now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.