Skip to content

Instantly share code, notes, and snippets.

@SeavantUUz
Created March 28, 2014 11:32
Show Gist options
  • Save SeavantUUz/9830668 to your computer and use it in GitHub Desktop.
Save SeavantUUz/9830668 to your computer and use it in GitHub Desktop.
function getPathTo(element) {
if (element.id!=='')
return 'id("'+element.id+'")';
if (element===document.body)
return element.tagName;
var ix= 0;
var siblings= element.parentNode.childNodes;
for (var i= 0; i<siblings.length; i++) {
var sibling= siblings[i];
if (sibling===element)
return getPathTo(element.parentNode)+'/'+element.tagName+'['+(ix+1)+']';
if (sibling.nodeType===1 && sibling.tagName===element.tagName)
ix++;
}
}
var getElementByXpath = function (path) {
return document.evaluate(path, document, null, 9, null).singleNodeValue;
};
$(function(){
$('*').bind('click',function(event){
var x = event.pageX - window.pageXOffset;
var y = event.pageY - window.pageYOffset;
element = document.elementFromPoint(x,y);
var path = getPathTo(element);
console.log(path);
var _element = getElementByXpath(path);
console.log(_element);
event.stopPropagation();
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment