Last active
August 29, 2015 14:04
-
-
Save saarmstrong/8697947426ae333ae4bf to your computer and use it in GitHub Desktop.
jQuery getSelector Plugin: returns current element's selector string
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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