Skip to content

Instantly share code, notes, and snippets.

@robrighter
Forked from weaver/make-selector.js
Created October 27, 2010 02:05
Show Gist options
  • Save robrighter/648261 to your computer and use it in GitHub Desktop.
Save robrighter/648261 to your computer and use it in GitHub Desktop.
function makeSelector(el) {
var tag, index, stack = [];
while (el) {
tag = el.tagName;
index = 0
while(el && el.previousSibling){
el = el.previousSibling;
if (tag == el.tagName)
index += 1;
}
if(el){
if(tag)
stack.unshift(tag + ':eq(' + (index + 0) + ')');
el = el.parentNode;
}
}
return stack.join(' > ');
}
@gf3
Copy link

gf3 commented Oct 27, 2010

This seems pretty expensive, why not just give it a unique ID?

@robrighter
Copy link
Author

It's for use in situations where I have a read only copy of the DOM.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment