-
Notifications
You must be signed in to change notification settings - Fork 22.4k
/
index.md
68 lines (49 loc) · 1.45 KB
/
index.md
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
---
title: "XPathResult: singleNodeValue property"
short-title: singleNodeValue
slug: Web/API/XPathResult/singleNodeValue
page-type: web-api-instance-property
browser-compat: api.XPathResult.singleNodeValue
---
{{APIRef("DOM XPath")}}
The read-only **`singleNodeValue`** property of the
{{domxref("XPathResult")}} interface returns a {{domxref("Node")}} value or
`null` in case no node was matched of a result with
{{domxref("XPathResult.resultType")}} being `ANY_UNORDERED_NODE_TYPE` or
`FIRST_ORDERED_NODE_TYPE`.
## Value
The return value is the {{domxref("Node")}} value of the `XPathResult`
returned by {{domxref("Document.evaluate()")}}.
### Exceptions
#### TYPE_ERR
In case {{domxref("XPathResult.resultType")}} is not
`ANY_UNORDERED_NODE_TYPE` or `FIRST_ORDERED_NODE_TYPE`, an
{{domxref("XPathException")}} of type `TYPE_ERR` is thrown.
## Examples
The following example shows the use of the `singleNodeValue` property.
### HTML
```html
<div>XPath example</div>
<div>
Tag name of the element having the text content 'XPath example':
<output></output>
</div>
```
### JavaScript
```js
const xpath = "//*[text()='XPath example']";
const result = document.evaluate(
xpath,
document,
null,
XPathResult.FIRST_ORDERED_NODE_TYPE,
null,
);
document.querySelector("output").textContent = result.singleNodeValue.localName;
```
### Result
{{EmbedLiveSample('Examples', 400, 70)}}
## Specifications
{{Specifications}}
## Browser compatibility
{{Compat}}