If I have a set of elements:
Text 1
I think this will work for you.
var deepestLevel = 0;
var deepestLevelText = "";
function findDeepNested(element, currentLevel) {
if ((element.children().length == 0) && (deepestLevel < currentLevel)) {
// No children and current level is deeper than previous most nested level
deepestLevelText="- " + element.text() + "
";
}
else { // there are children, keep diving
element.children().each( function () {
findDeepNested($(this), currentLevel + 1);
});
}
}
$(".start").each( function () {
deepestLevel = 0;
deepestLevelText = "";
findDeepNested($(this), 0);
$("#results").append(deepestLevelText);
});
Fiddle