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 Traversal – end() vs. No end()</title> <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script> <style> body { font-family: Arial; background: #f9f9f9; padding: 30px; } h2 { color: #2c3e50; } .btn { padding: 10px 20px; margin-right: 10px; margin-bottom: 20px; color: #fff; border: none; border-radius: 4px; cursor: pointer; } .btn-noend { background: #e74c3c; } .btn-end { background: #27ae60; } .container { border: 2px solid #bdc3c7; padding: 15px; margin-bottom: 20px; background: #fff; } .item { display: inline-block; margin: 5px; padding: 10px 15px; background: #dfe6e9; border-radius: 4px; } .highlight-child { background: #74b9ff; color: #fff; } .highlight-parent { border-color: #fab1a0; background: #ffeaa7; } </style> </head> <body> <h2>jQuery Traversal – <code>end()</code> vs. No <code>end()</code></h2> <p>Use the buttons below to highlight <strong>items</strong> and their <strong>containers</strong>. The second button uses <code>end()</code> to “rewind” the chain.</p> <button class="btn btn-noend" id="noEnd">No <code>end()</code></button> <button class="btn btn-end" id="withEnd">With <code>end()</code></button> <div class="container"> <div class="item">Item A1</div> <div class="item">Item A2</div> </div> <div class="container"> <div class="item">Item B1</div> <div class="item">Item B2</div> </div> <script> $(function() { function reset() { $('.item').removeClass('highlight-child highlight-parent'); $('.container').removeClass('highlight-parent'); } $('#noEnd').click(function(){ reset(); // highlights items correctly, but also (incorrectly) gives them the parent highlight $('.container') .find('.item') .addClass('highlight-child') .addClass('highlight-parent'); }); $('#withEnd').click(function(){ reset(); // highlights items, then .end() rewinds back to containers before adding parent highlight $('.container') .find('.item').addClass('highlight-child') .end().addClass('highlight-parent'); }); }); </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