jQuery | detach() with Examples
The detach() is an inbuilt method in jQuery that removes the selected elements from the DOM tree including its all text and child nodes but it keeps the data and the events. Document Object Model (DOM) is a World Wide Web Consortium standard. This defines for accessing elements in the DOM tree.
Syntax:
$(selector).detach()
Parameter:It does not accept any parameter.
Return Value: It returns the selected element with all removed texts and child nodes.
Code #1:
In the below code, all the paragraph element will detached.
<html> <head> </script> <script> <!-- jQuery code to show detach method working --> $(document).ready(function() { $("button").click(function() { $("p").detach(); }); }); </script> <style> body { display: block; width: 400px; height: 250px; padding: 20px; border: 2px solid green; font-size: 25px; } </style> </head> <body> <div> This is the div part !</div> <br> <!-- This paragraphs get detached --> <p>This is the first paragraph !</p> <p>This is the second paragraph !</p> <button>Click Me !</button> </body> </html> |
Output:
Before clicking the “click Me” button-

After clicking the “click Me” button-

Recommended Posts:
- jQuery | val() with Examples
- jQuery | after() with Examples
- jQuery | on() with Examples
- jQuery | last() with Examples
- jQuery | one() with Examples
- jQuery | has() with Examples
- jQuery | eq() with Examples
- jQuery | first() with Examples
- jQuery | hide() with Examples
- jQuery | fadeTo() with Examples
- jQuery | queue() with Examples
- jQuery | hasClass() with Examples
- jQuery | click() with Examples
- jQuery | addClass() with Examples
- jQuery | stop() with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



