jQuery detach()

The jQuery detach() method is used to remove the selected elements, including all texts and child nodes and keeps only data and events.

This method saves a copy of the removed elements to reinsert them whenever they needed later.

There are some other methods also which are used to remove elements e.g. jQuery remove() method, jQuery empty() method etc. But there is a little difference among them.

jQuery remove() method: This method is used to remove the elements as well as its data and events.

jQuery empty() method: This method is used to remove only the content from the selected elements.

Syntax:

Example of jQuery detach() method

Let's take an example to demonstrate the effect of jQuery detach() method.

Test it Now

jQuery detach() example 2

Test it Now

Difference between detach() and remove() method

Let's take an example to clear the difference between detach() and remove() method:

jQuery detach() example 3

Test it Now

By the above example, it is clear that jQuery detach() method doesn't remove inner data and events. In the above example, the click event is remained safe even after the detach() method is applied.





Latest Courses