Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

jQuery | detach() with Examples

  • Last Updated : 13 Feb, 2019

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.


Parameter:It does not accept any parameter.
Return Value: It returns the selected element with all removed texts and child nodes.

jQuery code to show the working of this method:

Code #1:
In the below code, all the paragraph element will detached.

Hey geek! The constant emerging technologies in the world of web development always keeps the excitement for this subject through the roof. But before you tackle the big projects, we suggest you start by learning the basics. Kickstart your web development journey by learning JS concepts with our JavaScript Course. Now at it's lowest price ever!

        <!-- jQuery code to show detach method working -->
        $(document).ready(function() {
            $("button").click(function() {
        body {
            display: block;
            width: 400px;
            height: 250px;
            padding: 20px;
            border: 2px solid green;
            font-size: 25px;
    <div> This is the div part !</div>
    <!-- This paragraphs get detached -->
    <p>This is the first paragraph !</p>
    <p>This is the second paragraph !</p>
    <button>Click Me !</button>

Before clicking the “click Me” button-

After clicking the “click Me” button-

My Personal Notes arrow_drop_up
Recommended Articles
Page :