<div>XPath example</div>
<div>
Tag name of the element having the text content 'XPath example':
<output></output>
</div>XPathResult: singleNodeValue property / notebook
XPathResult: singleNodeValue property
Runnable notebook: every JavaScript, HTML and CSS example on this page executes in a sandboxed local frame. Nothing leaves the browser.
const xpath = "//*[text()='XPath example']";
const result = document.evaluate(
xpath,
document,
null,
XPathResult.FIRST_ORDERED_NODE_TYPE,
null,
);
document.querySelector("output").textContent = result.singleNodeValue.localName;