Remove all <p> elements:
<!DOCTYPE html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"> </script>//from www . j a va 2s .c o m <script> $(document).ready(function(){ $("button").click(function(){ $("p").detach(); }); }); </script> </head> <body> <p>This is a paragraph.</p> <p>This is another paragraph.</p> <button>Remove all p elements</button> </body> </html>
The detach()
method removes the selected elements, including its child nodes.
The detach()
method keeps its data and events.
This method keeps a copy of the removed elements, we can reinserted them back.
$(selector).detach()