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 &lt;div&gt;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

Browser compatibility

See also

Help improve MDN

Learn how to contribute

This page was last modified on by MDN contributors.