-
Notifications
You must be signed in to change notification settings - Fork 22.4k
/
index.md
175 lines (131 loc) · 4.55 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
---
title: "Element: querySelector() method"
short-title: querySelector()
slug: Web/API/Element/querySelector
page-type: web-api-instance-method
browser-compat: api.Element.querySelector
---
{{APIRef("DOM")}}
The **`querySelector()`** method of the {{domxref("Element")}}
interface returns the first element that is a descendant of the element on which it is
invoked that matches the specified group of selectors.
## Syntax
```js-nolint
querySelector(selectors)
```
### Parameters
- `selectors`
- : A group of [selectors](/en-US/docs/Learn/CSS/Building_blocks/Selectors) to match
the descendant elements of the {{domxref("Element")}} `baseElement`
against; this must be valid CSS syntax, or a `SyntaxError` exception will
occur. The first element found which matches this group of selectors is returned.
### Return value
The first descendant element of `baseElement` which matches the specified
group of `selectors`. The entire hierarchy of elements is considered when
matching, including those outside the set of elements including `baseElement`
and its descendants; in other words, `selectors` is first applied to the
whole document, not the `baseElement`, to generate an initial list of
potential elements. The resulting elements are then examined to see if they are
descendants of `baseElement`. The first match of those remaining elements is
returned by the `querySelector()` method.
If no matches are found, the returned value is `null`.
### Exceptions
- `SyntaxError` {{domxref("DOMException")}}
- : Thrown if the specified `selectors` are invalid.
## Examples
Let's consider a few examples.
### Find a specific element with specific values of an attribute
In this first example, the first {{HTMLElement("style")}} element which either has no
type or has type "text/css" in the HTML document body is returned:
```js
const el = document.body.querySelector(
"style[type='text/css'], style:not([type])",
);
```
### Get direct descendants using the :scope pseudo-class
This example uses the {{cssxref(":scope")}} pseudo-class to retrieve direct children of the `parentElement` element.
#### HTML
```html
<div>
<h6>Page Title</h6>
<div id="parent">
<span>Love is Kind.</span>
<span>
<span>Love is Patient.</span>
</span>
<span>
<span>Love is Selfless.</span>
</span>
</div>
</div>
```
#### CSS
```css
span {
display: block;
margin-bottom: 5px;
}
.red span {
background-color: red;
padding: 5px;
}
```
#### JavaScript
```js
const parentElement = document.querySelector("#parent");
let allChildren = parentElement.querySelectorAll(":scope > span");
allChildren.forEach((item) => item.classList.add("red"));
```
#### Result
{{ EmbedLiveSample('Get_direct_descendants_using_the_scope_pseudo-class', 600, 160) }}
### The entire hierarchy counts
This example demonstrates that the hierarchy of the entire document is considered when
applying `selectors`, so that levels outside the specified
`baseElement` are still considered when locating matches.
#### HTML
```html
<div>
<h5>Original content</h5>
<p>
inside paragraph
<span>inside span</span>
inside paragraph
</p>
</div>
<div>
<h5>Output</h5>
<div id="output"></div>
</div>
```
#### JavaScript
```js
const baseElement = document.querySelector("p");
document.getElementById("output").innerHTML =
baseElement.querySelector("div span").innerHTML;
```
#### Result
The result looks like this:
{{ EmbedLiveSample('The_entire_hierarchy_counts', 600, 160) }}
Notice how the `"div span"` selector still successfully matches the
{{HTMLElement("span")}} element, even though the `baseElement`'s child nodes
do not include the {{HTMLElement("div")}} element (it is still part of the specified
selector).
### More examples
See {{domxref("Document.querySelector()")}} for additional examples of the proper
format for the `selectors`.
## Specifications
{{Specifications}}
## Browser compatibility
{{Compat}}
## See also
- [Locating DOM elements using selectors](/en-US/docs/Web/API/Document_Object_Model/Locating_DOM_elements_using_selectors)
- [Attribute selectors](/en-US/docs/Web/CSS/Attribute_selectors) in the CSS
Guide
- [Attribute selectors](/en-US/docs/Learn/CSS/Building_blocks/Selectors/Attribute_selectors) in the MDN Learning Area
- {{domxref("Element.querySelectorAll()")}}
- {{domxref("Document.querySelector()")}} and
{{domxref("Document.querySelectorAll()")}}
- {{domxref("DocumentFragment.querySelector()")}} and
{{domxref("DocumentFragment.querySelectorAll()")}}
- Other methods that take selectors: {{domxref("element.closest()")}} and
{{domxref("element.matches()")}}.