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 Traversing Ancestors - parentsUntil() Method Example</title> <style> body { font-family: Arial, sans-serif; padding: 40px; background-color: #f0f4f8; } h1 { color: #1a237e; } p { font-size: 15px; color: #37474f; margin-bottom: 20px; } .controls { margin-bottom: 20px; } .controls button { margin-right: 10px; padding: 8px 16px; background-color: #3949ab; color: #fff; border: none; border-radius: 4px; cursor: pointer; font-size: 14px; } .controls button:hover { background-color: #303f9f; } .box { padding: 15px; margin: 10px; border: 2px solid #1E88E5; border-radius: 4px; background: #e3f2fd; position: relative; } .highlight { background-color: #ffe082 !important; /* warm yellow */ } .info { margin-top: 20px; padding: 12px 16px; background: #ffffff; border: 1px solid #ccc; border-radius: 4px; color: #263238; font-size: 14px; display: inline-block; } </style> <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script> </head> <body> <h1>jQuery Traversing Ancestors - <code>parentsUntil()</code> Method</h1> <p>Click a button to highlight ancestor boxes of the innermost “leaf” element, stopping before the specified selector.</p> <div class="controls"> <button id="btn-level2">Until <code>.level2</code></button> <button id="btn-level3">Until <code>.level3</code></button> <button id="btn-level4">Until <code>.level4</code></button> <button id="btn-level5">Until <code>.level5</code></button> </div> <div class="box outer level5" id="level5"> Level 5 (Outermost) <div class="box level4" id="level4"> Level 4 <div class="box level3" id="level3"> Level 3 <div class="box level2" id="level2"> Level 2 <div class="box leaf" id="leaf">Leaf Element (Innermost)</div> </div> </div> </div> </div> <div class="info" id="infoBox">Click a button to see <code>parentsUntil()</code> in action.</div> <script> $(function() { function clearHighlights() { $('.box').removeClass('highlight'); } function highlightUntil(selector) { clearHighlights(); var $anc = $('#leaf').parentsUntil(selector); $anc.addClass('highlight'); var ids = $anc.map(function() { return this.id; }).get().join(' → '); $('#infoBox').text( ids ? 'Highlighted ancestors: ' + ids : 'No ancestors before ' + selector ); } $('#btn-level2').click(function() { highlightUntil('.level2'); }); $('#btn-level3').click(function() { highlightUntil('.level3'); }); $('#btn-level4').click(function() { highlightUntil('.level4'); }); $('#btn-level5').click(function() { highlightUntil('.level5'); }); }); </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