27 of 119 menu

The detach method

The detach method deletes selected elements with the ability to restore them. The method is convenient when we want to insert the deleted elements later.

Syntax

This is how we remove elements. Additionally, we can filter the selected elements by passing the optional parameter selector to the method:

.detach([selector]);

Example

Let's remove the paragraph with #test using the detach method and restore it using the appendTo method to insert the element into div:

<p id="test">text</p> <div></div> let $elem = $('#test'); $elem.detach(); // delete element $elem.appendTo('div'); // insert the element back into the page

HTML the code will look like this:

<div><p id="test">text</p></div>

See also

  • method remove,
    which deletes elements without the possibility of their recovery
  • method empty,
    which removes content from the selected elements
  • JavaScript method remove,
    which allows you to delete an element
azbydeenesfrkakkptruuz