var $es = []; var findLeaves = function(i, $root) { console.log($root); $children = $root.children(); if($children.length > 0) { $children.each(findLeaves); } else { $es.push($root); } } var rr = jQuery('#section-summary'); findLeaves(0, rr);