XPathEvaluator: XPathEvaluator() constructor

The XPathEvaluator() constructor creates a new XPathEvaluator.

Syntax

js
new XPathEvaluator()

Parameters

None.

Return value

A new XPathEvaluator object.

Examples

Count the number of <div> elements

The following example shows the use of the XPathEvaluator interface.

HTML

html
<div>XPath example</div>
<div>Number of &lt;div&gt; elements: <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 Standard
# dom-xpathevaluator-xpathevaluator

Browser compatibility

BCD tables only load in the browser