Codelab Editor
Free Online HTML Editor
Run
Learn jQuery
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0"/> <title>jQuery detach() Method</title> <style> body { font-family: Arial, sans-serif; padding: 40px; background-color: #f9f9f9; } h1 { color: #333; } p { font-size: 16px; color: #444; } .content-box { padding: 20px; background-color: #fff; border: 1px solid #ccc; border-radius: 6px; margin-top: 20px; box-shadow: 0 2px 5px rgba(0,0,0,0.05); } .detach-target { padding: 15px; background-color: #e3f2fd; border: 1px solid #2196F3; border-radius: 4px; margin-top: 15px; cursor: pointer; } button { margin-top: 10px; padding: 10px 20px; background-color: #007BFF; color: white; font-size: 14px; border: none; border-radius: 4px; cursor: pointer; } button:hover { background-color: #0056b3; } .button-group { margin-top: 15px; } </style> </head> <body> <h1>jQuery detach() Method</h1> <p> The <code>detach()</code> method removes selected elements from the DOM while preserving jQuery data and bound events. This makes it useful when you plan to reinsert the element later. </p> <div class="content-box" id="container"> <div class="detach-target" id="box"> <strong>Click me — I still respond after being detached!</strong><br> This box is detachable and reusable. </div> <div class="button-group"> <button id="detachBtn">Detach Element</button> <button id="reattachBtn">Re-Attach Element</button> </div> </div> <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script> <script> let detachedElement; $(document).ready(function() { // Original event $('#box').on('click', function() { alert('Event still works even after detach!'); }); $('#detachBtn').click(function() { detachedElement = $('#box').detach(); // Remove but preserve data & events }); $('#reattachBtn').click(function() { if (detachedElement) { $('#container').prepend(detachedElement); // Reinsert at the beginning } }); }); </script> </body> </html>
Editor Commands
Ctrl
F
Find
Ctrl
G
Find Next
Ctrl
⇧
G
Find Previous
Ctrl
⇧
F
Find & Replace
Ctrl
]
Indent Code Right
Ctrl
[
Indent Code Left
⇧
Tab
Auto Indent Code
Ctrl
/
Line Comment
Ctrl
Alt
/
Block Comment
TutsInsider Actions
Alt
S
Run Code
Ctrl
C
Copy Editor Code
Ctrl
A
Select All Editor Code
Ctrl
S
Save/Download
Ctrl
Alt
X
Rotate
Ctrl
Alt
T
Switch Theme
Ctrl
⇧
C
Open Console
Esc
Close Console