Skip to content

Instantly share code, notes, and snippets.

@shobhit
Created October 19, 2022 05:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shobhit/38115185b2868e1061407a6a2a14ce93 to your computer and use it in GitHub Desktop.
Save shobhit/38115185b2868e1061407a6a2a14ce93 to your computer and use it in GitHub Desktop.
const getCssSelectorShort = (el) => {
let path = [], parent;
while (parent = el.parentNode) {
let tag = el.tagName, siblings;
path.unshift(
el.id ? `#${el.id}` : (
siblings = parent.children,
[].filter.call(siblings, sibling => sibling.tagName === tag).length === 1 ? tag :
`${tag}:nth-child(${1+[].indexOf.call(siblings, el)})`
)
);
el = parent;
};
return `${path.join(' > ')}`.toLowerCase();
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment