JQuery detach() is used to remove the selected elements including all text and child nodes from the DOM. This method is same as remove method except it will keep all data and events associated with the removed elements even after it is removed from the DOM(Document Object Model). It also keeps copy of removed elements which can be re-inserted at a later time into the DOM.
JQuery Detach() Syntax
It does not contain any parameters.
JQuery Detach() Example
- $(“button”).click(function()) line defines click event which occurs when button is clicked.
- $(“h3,h4″).detach(); line defines detach() method which removes the all elements from the DOM.
- When we run the above script, a button will appear on the browser. If we click on the button, all elements will get removed from the DOM.
When you run the above example, you would get the following output :