Home >>jQuery Tutorial >jQuery detach() Method

jQuery detach() Method

jQuery detach() Method

jQuery detach() method is used to removes the selected elements, including all child nodes and text and also it keeps data and events.

It is also keeps a copy of the removed elements, which allows them at a later time to be reinserted.

Syntax:
$(selector).detach()
Here is an Example of jQuery detach() Method:

<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$(".btn1").click(function(){
$(".txt1").detach();
});
});
</script>
</head>
<body>
<p class="txt1">This is a PHPTPOINT.</p>
<p class="txt1">This is a paragraph.</p>
<button class="btn1">click me </button>
</body>
</html>

Output:

This is a PHPTPOINT.

This is a paragraph.