JSFiddle - React, Tailwind, and code Playground
HTML
<div>
<span id="something"></span>
</div>
JavaScript
var a = $('#something');
// how do I get the xPath of this jQuery elemen, U have the below function to reuse
alert(XPath(a));
function XPath(elm) {
for (segs = []; elm && elm.nodeType == 1; elm = elm.parentNode) {
if (elm.hasAttribute('id')) {
segs.unshift('id("' + elm.getAttribute('id') + '")')
return segs.join('/')
}
else if (elm.hasAttribute('class'))
segs.unshift(elm.localName.toLowerCase() + '[@class="' + elm.getAttribute('class') + '"]')
else {
for (i = 1, sib = elm.previousSibling; sib; sib = sib.previousSibling)
if (sib.localName == elm.localName) i++
segs.unshift(elm.localName.toLowerCase() + '[' + i + ']')
}
}
return segs.length ? '/' + segs.join('/') : null
}