jQuery detach()
×


jQuery detach()

1250

  • This jQuery method is used to remove the selected element with its child elements and event handlers form the web page.
  • This method prepares a copy of the removed elements to reinsert them in the web page.
Note:

If you want to remove all child elements and their content of the selected element then use, empty() jQuery method.


Related Topics:

jQuery prepend
jQuery prependTo

Note:

If you want to remove the selected element with its child elements and event handlers form the web page then use remove() jQuery method.

jQuery detach

Syntax:

$(selector).detach();

Example:

<html>
<head>
<title> jQuery Example </title>
<script type = "text/javascript"
src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js">
</script>
<style>
.para{
background:yellow;
padding:10px;
}
.head{
background:pink;
padding:10px;
}
button{
background:green;
padding:10px;
color:white;
border:1px solid green;
}
</style>
<script>
$(document).ready(function(){
$(".b1").click(function(){
$content=$("div").detach();
});

$(".b2").click(function(){
$($content).insertBefore(".b1");
});

});
</script>
</head>
<body>

<h2> jQuery detach() Method Example </h2>
<div>
<h3 class="head"> This is heading </h3>
<p class="para"> This paragraph. </p>
</div>
<button class="b1"> Click to remove! </button> | <button class="b2"> Click to restore! </button>
</body>
</html>

Output:

When you click the remove button,

When you click on restore button, this method will add again all child elements of div element.



Best WordPress Hosting


Share:


Discount Coupons

Get a .COM for just $6.98

Secure Domain for a Mini Price



Leave a Reply


Comments
    Waiting for your comments