JSFiddle - React, Tailwind, and code Playground

HTML

<div id="bar">
    <div>
        <div id="foo">
           <div id="foo1">
              <div id="foo2"></div>
           </div>
        </div>
    </div>
</div>

JavaScript

(function($) {
    $.fn.parentNth = function(n) {
        var el = $(this);
        for(var i = 0; i < n; i++)
            el = el.parent();
        
        return el;
    };
})(jQuery);

alert($("#foo").parentNth(2).attr("id"));