JSFiddle - React, Tailwind, and code Playground

HTML

<div id="div">
    <div></div>
    <div></div>
    <div>
        <div></div>
        <div></div>
        <div></div>
    </div>
    <div></div>
    <div>
        <div></div>
        <div></div>
    </div>
</div>

CSS

div { padding-left: 20px }

JavaScript

$.fn.goDeep = function(levels, func){
    var iterateChildren = function(current, levelsDeep){
        func.call(current, levelsDeep)
        
        if(levelsDeep > 0)
            $.each(current.children(), function(index, element){
                iterateChildren($(element), levelsDeep-1);
            })
    };
    
    return this.each(function(){
        iterateChildren($(this), levels);
    });
    
};
    
$('#div').goDeep(3, function(deep){
    $(this).append(Math.abs(deep - (3+1)));
})