JSFiddle - React, Tailwind, and code Playground

by ihope

JavaScript

var path;
$$("body")[0].onclick = function(e){
    var node = e.target;
    
    var s = "";
    while (node.parentNode){
        var Ind = 1;
        prevSibling = node;
        while (prevSibling.previousElementSibling && prevSibling.previousElementSibling.tagName == prevSibling.tagName){
            Ind += 1;
            prevSibling = prevSibling.previousElementSibling
        };
       var attributesSelector = [] ;
        if (node.className){
            attributesSelector.push("@class='" + node.className + "'");
        }
        if (node.id){
            attributesSelector.push("@id='" + node.id + "'" );
        }
        if (Ind > 1){
            attributesSelector.push( Ind );
        }
        attributesSelector = attributesSelector.length ?  "["+attributesSelector.join(" and ") + "]" : "";
        s += node.tagName + attributesSelector + "\\";
        node = node.parentNode
    };
    
    path = s.split("\\").reverse().join("/");
    console.log(path);
}