XPathEvaluator: XPathEvaluator() constructor

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.

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