Skip to content

Instantly share code, notes, and snippets.

@sandipchitale
Last active June 14, 2020 09:04
Show Gist options
  • Save sandipchitale/3a4f5143d6592f8514d1d4015aaefb96 to your computer and use it in GitHub Desktop.
Save sandipchitale/3a4f5143d6592f8514d1d4015aaefb96 to your computer and use it in GitHub Desktop.
Get xPath of document.activeElement
{ let e = document.activeElement; let pchain = []; while (e.tagName !== 'BODY') { pchain.unshift(e.tagName + '[' + (Array.prototype.indexOf.call(e.parentElement.querySelectorAll(':scope > ' + e.tagName), e) + 1) + ']'); e = e.parentElement; } ; '/html/body/' + pchain.join('/').toLowerCase() }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment