JQuery Detach Method() Example

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.

As shown in the above program, we have used the code inside $(document).ready which is an event which fires up when document is ready. It will run once the page document object model is ready for JavaScript code to execute.

$(“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.

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

About Us

Javabeat.net is a blog dedicated to Java/J2EE developers. This site focus on beginners and advanced developers. We constantly write fresh content on J2EE topics like Spring, Hibernate and wide variety of J2EE frameworks.