<div>XPath example</div>
<div>Tag names of the matched nodes: <output></output></div>XPathResult: iterateNext() method / notebook
XPathResult: iterateNext() method
Runnable notebook: every JavaScript, HTML and CSS example on this page executes in a sandboxed local frame. Nothing leaves the browser.
const xpath = "//div";
const result = document.evaluate(
xpath,
document,
null,
XPathResult.ANY_TYPE,
null,
);
let node = null;
const tagNames = [];
while ((node = result.iterateNext())) {
tagNames.push(node.localName);
}
document.querySelector("output").textContent = tagNames.join(", ");