Skip to content

Instantly share code, notes, and snippets.

@zazaulola
Created October 7, 2021 16:04
Show Gist options
  • Save zazaulola/9f2cee988069f42c0eed1c563e1b4e9c to your computer and use it in GitHub Desktop.
Save zazaulola/9f2cee988069f42c0eed1c563e1b4e9c to your computer and use it in GitHub Desktop.
function getSelector(el){
let nodes = [],c,e,d=el.ownerDocument,h=d.documentElement;
do{if (el.id){ nodes.push(`#${el.id}`); break;
}else{ if (el==h) c=0; else for (c=1,e=el;e=e.previousElementSibling;c++);
nodes.push(c?`:nth-child(${c})`:el.tagName.toLowerCase());
} } while ((el=el.parentNode)&&el!=d); return nodes.reverse().join` > `;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment