Skip to content

Instantly share code, notes, and snippets.

@saarmstrong
Last active August 29, 2015 14:04
Show Gist options
  • Save saarmstrong/8697947426ae333ae4bf to your computer and use it in GitHub Desktop.
Save saarmstrong/8697947426ae333ae4bf to your computer and use it in GitHub Desktop.
jQuery getSelector Plugin: returns current element's selector string
(function($){
$.fn.getSelector = function() {
element = this[0];
var str = '';
while(element && element.nodeType == 1) {
var id = $(element.parentNode).children(element.tagName).index(element) + 1;
id > 1 ? (id = ':nth-child(' + id + ')') : (id = '');
if(str.length > 0) {
str = ' > ' + str;
}
str = element.tagName.toLowerCase() + id + str;
element = element.parentNode;
}
return str;
};
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment