Saturday, October 18, 2025
HomeLanguagesJavascriptjQuery detach() Method

jQuery detach() Method

The detach() is an inbuilt method in jQuery that removes the selected elements from the DOM tree including 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 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.

jQuery code to show the working of this method:

Example 1: In the below code, all the paragraph elements will detached.

html




<html>
 
<head>
    <script src=
    </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:

jquery-10

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS