CodeLab
Show Output
 
 
​x
 
1
<!DOCTYPE html>
2
<html lang="en">
3
<head>
4
<meta charset="utf-8">
5
<title>jQuery detach()</title>
6
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" />
7
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
8
<style>
9
.highlight{font-size:18px;color:#11a286}
10
</style>
11
<p><b>remove()</b></p>  
12
 <form>
13
    <div id="dvParent">
14
       <div id="divjQuery">lorem ipsum dolor sit.</div>
15
    </div>
16
    <br/>
17
    <input type='button' id='btnRemove' Value='Remove Div' />
18
    <input type='button' id='btnAdd' Value='Add Div' />
19
    <br/><br/>
20
</form>
21
<script>
22
$(document).ready(function() {
23
    var divjQuery = null;
24
    $('#btnAdd').attr('disabled','disabled');
25
    
26
    $("#divjQuery").hover(function() {
27
        $(this).addClass('highlight');
28
    }, function() {
29
        $(this).removeClass('highlight');
30
    });
31
​
32
    $('#btnRemove').click(function() {
33
        divjQuery = $("#divjQuery").remove();
34
        $(this).attr('disabled','disabled');
35
        $('#btnAdd').removeAttr('disabled');
36
    });
37
    
38
    $('#btnAdd').click(function() {
39
        $("#dvParent").html(divjQuery);
40
         $(this).attr('disabled','disabled');
41
        $('#btnRemove').removeAttr('disabled');
42
    });
43
    
44
});
45
</script>
46
 
47
<p><b>detach()</b></p>  
48
<form>
49
    <div id="divParent">
50
   <div id="divjQueryDetach">lorem ipsum dolor sit. </div>
51
    </div>
52
    <br/>
53
    <input type='button' id='btnDetach' Value='Detach Div' />
54
    <input type='button' id='btnAttach' Value='Re-attach Div' />
55
    <br/><br/>
56
</form>
57
<script>
58
$(document).ready(function() {
59
    var divjQueryDetach = null;
60
    $('#btnAttach').attr('disabled','disabled');
61
    
62
    $("#divjQueryDetach").hover(function() {
63
        $(this).addClass('highlight');
64
    }, function() {
65
        $(this).removeClass('highlight');
66
    });
67
​
68
    $('#btnDetach').click(function() {
69
        divjQueryDetach = $("#divjQueryDetach").detach();
70
        $(this).attr('disabled','disabled');
71
        $('#btnAttach').removeAttr('disabled');
72
    });
73
    
74
    $('#btnAttach').click(function() {
75
        $("#divParent").html(divjQueryDetach);
76
         $(this).attr('disabled','disabled');
77
        $('#btnDetach').removeAttr('disabled');
78
    });
79
});
80
</script>
81
</body>
82
</html>                            
 

Share this example with Facebook, Twitter