Skip to content

Instantly share code, notes, and snippets.

@abidibo
Created March 28, 2013 11:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save abidibo/5262403 to your computer and use it in GitHub Desktop.
Save abidibo/5262403 to your computer and use it in GitHub Desktop.
Reverse selector engineering. Get css selector from element. Works with browsers implementing the :nth-of-type selector. Requires mootools selectors.
// requires mootools selectors
var path = function path(element) {
if(element.get('id')) {
return '#' + element.get('id');
}
if(element == document.body) {
return 'body';
}
var prev_equal_siblings_length = element.getAllPrevious(element.get('tag')).length;
var part = element.get('tag') + ':nth-of-type(' + (prev_equal_siblings_length + 1) + ')';
return path(element.getParent()) + ' > ' + part;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment