JSFiddle - React, Tailwind, and code Playground
HTML
<div>
<ul>
<li>item 1</li>
<li>item 2</li>
</ul>
</div>
<div>
<ul>
<li>item 1</li>
<li>item 2</li>
</ul>
</div>
<Br />
<span style="font-size:75%;" id="path"></span>
JavaScript
(function($){
$.fn.extend({
getFullPath: function(stopAtBody){
stopAtBody = stopAtBody || false;
function traverseUp(el){
var result = el.tagName + ':eq(' + $(el).index() + ')',
pare = $(el).parent()[0];
if (pare.tagName !== undefined && (!stopAtBody || pare.tagName !== 'BODY')){
result = [traverseUp(pare), result].join(' ');
}
return result;
};
return this.length > 0 ? traverseUp(this[0]) : '';
}
});
})(jQuery);
$('li').click(function(){
var fullPath = $(this).getFullPath(),
fullUntilBody = $(this).getFullPath(true);
// just to verify, color the element by this fullPath selector
$(fullUntilBody).css('color','#FF00FF');
$('#path').text('Standard: ' + fullPath + ' -- From Body: ' + fullUntilBody);
});