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 undelegate() Method</title> <style> body { font-family: Arial, sans-serif; padding: 40px; background-color: #f2f2f2; } 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); } #container { padding: 15px; background-color: #E8F5E9; border: 2px dashed #43A047; border-radius: 6px; margin-bottom: 15px; color: #2E7D32; } .item { padding: 8px 10px; margin: 5px 0; background-color: #A5D6A7; border-radius: 4px; cursor: pointer; } button { padding: 10px 20px; background-color: #43A047; color: white; font-size: 14px; border: none; border-radius: 4px; cursor: pointer; margin-right: 10px; } button:hover { background-color: #2E7D32; } </style> <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script> </head> <body> <h1>jQuery undelegate() Method</h1> <p> The <code>undelegate()</code> method removes event handlers attached using <code>delegate()</code>. This example demonstrates how clicking on items triggers an alert, and how that behavior is removed using <code>undelegate()</code>. </p> <div class="content-box"> <div id="container"> <div class="item">Item 1</div> <div class="item">Item 2</div> <div class="item">Item 3</div> </div> <button id="removeEvent">Remove Delegated Click Event</button> <button id="addItem">Add New Item</button> </div> <script> $(document).ready(function() { // Delegate click event to container $('#container').delegate('.item', 'click', function() { alert($(this).text() + ' clicked!'); }); // Undelegate the click event $('#removeEvent').click(function() { $('#container').undelegate('.item', 'click'); alert('Delegated event removed.'); }); // Dynamically add new item $('#addItem').click(function() { const count = $('#container .item').length + 1; $('#container').append('<div class="item">Item ' + count + '</div>'); }); }); </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