Let's try and write one as a jQuery plugin - it should be trivial. All we need is to find the node's parents, and the node itself, and then generate a string containing the (lowercase) tag name, and (if supplied) the ID and classes:
(function($) {
function tag(el) {
var r = el.tagName.toLowerCase();
if (el.id) {
r += '#' + el.id;
}
if (el.className) {
r += '.' + el.className.replace(/\s+/g, '.');
}
return r;
}
$.fn.path = function() {
var node = this.get(0); // in case multiple elements are passed
return $(node).parents().add(node).get().map(tag).join(' ');
}
})(jQuery);
Trying that out here on this stackoverflow page gives:
$('.vote').path()
> "html body.question-page.new-topbar div.container div#content.snippet-hidden
div div#mainbar div#question.question table tbody tr td.votecell div.vote"