I am developing in javascript and I would need to get the xpath of the element clicked. I know that in order to get the id we can do :
element.onclick = function(event)
{
var target_id = event.target.id;
}
How could I do to get the xpath ?
Regards.
Here:
function getXPath(node){
if(node.hasAttribute("id")){
return '//' + node.tagName + '[@id="' + node.id + '"]';
}
if(node.hasAttribute("class")){
return '//' + node.tagName + '[@class="' + node.getAttribute("class") + '"]';
}
var old = '/' + node.tagName;
var new_path = this.xpath(node.parentNode) + old;
return new_path;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With