Reputation: 3856
If I have a structure like this:
<div>
<div>
<div>blah</div>
</div>
</div>
<div>
<div>
<div>
<div>blah</div>
</div>
</div>
</div>
Is there jquery to target the deepest elements (where the blahs are located)?
Upvotes: 0
Views: 1214
Reputation: 11731
A bit of recursive jQuery:
$(document).ready(function(){
alert(deepest($("div:first")).html());
function deepest(f){
if(f.find("div").length)return deepest(f.find("div"));
else return f;
}
});
Gets the first DIV with no children starting from whatever you pass to Deepest
Upvotes: 0
Reputation: 208032
$('div').each(function () {
if ($(this).children().length == 0) {
//do something
}
})
Upvotes: 1
Reputation: 27022
This will select anything that doesn't have any child elements:
$('div:not(:has(*))')
Upvotes: 4