jQuery Closest
by Rycochet
HTML
<div id="test1">
<div id="test2">
<span id="test3">
<div id="test4">
<div id="test5">
<div id="test6">
</div>
</div>
</div>
</span>
</div>
</div>
JavaScript
$(function() {
var el;
document.write('jQuery ' + $().jquery + '<br>');
el = $('#test1 #test6');
document.write('<br>1: ' + el.length + '<br>');
document.write('1a: ' + el.closest('span').attr('id') + '<br>');
document.write('1b: ' + el.first().closest('span').attr('id') + '<br>');
document.write('1c: ' + $(el[0]).closest('span').attr('id') + '<br>');
el = $('#test1 #test6');
document.write('<br>2: ' + el.length + '<br>');
document.write('2a: ' + el.closest('span').attr('id') + '<br>');
document.write('2b: ' + el.first().closest('span').attr('id') + '<br>');
document.write('2c: ' + $(el[0]).closest('span').attr('id') + '<br>');
el = $('#test1 #test3 #test6');
document.write('<br>3: ' + el.length + '<br>');
document.write('3a: ' + el.closest('span').attr('id') + '<br>');
document.write('3b: ' + el.first().closest('span').attr('id') + '<br>');
document.write('3c: ' + $(el[0]).closest('span').attr('id') + '<br>');
});