XPathExpression - Web APIs | MDN
Instance methods
XPathExpression.evaluate()-
Evaluates the XPath expression on the given node or document.
Example
The following example shows the use of the XPathExpression interface.
HTML
html
<div>XPath example</div>
<div>Number of <div>s: <output></output></div>
JavaScript
js
const xpath = "//div";
const evaluator = new XPathEvaluator();
const expression = evaluator.createExpression(xpath);
const result = expression.evaluate(
document,
XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,
);
document.querySelector("output").textContent = result.snapshotLength;
Result
Specifications
| Specification |
|---|
| DOM # interface-xpathexpression |