You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: 13-JS-In-the-Browser-DOM-Manipulation/README.md
+39-8Lines changed: 39 additions & 8 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -26,13 +26,13 @@ to go follow a tutorial online.
26
26
27
27
We can write JavaScript that can:
28
28
29
-
- Figure out how many h1's <h1></h1> are on a page
30
-
- How many input's <input></input> are in a form <form></form>
31
-
- Get the value from a form <form></form>
32
-
- If a user is typing into the form <form></form>
33
-
- Change the value of a <form></form>
34
-
- Change images <img></img>, make them bigger
35
-
- Change CSS styles <style></style> of anything
29
+
- Figure out how many h1's `<h1></h1> `are on a page
30
+
- How many input's `<input></input>` are in a form `<form></form>`
31
+
- Get the value from a form `<form></form>`
32
+
- If a user is typing into the form `<form></form>`
33
+
- Change the value of a `<form></form>`
34
+
- Change images `<img></img>`, make them bigger
35
+
- Change CSS styles `<style></style> `of anything
36
36
- Add animations
37
37
- Listen for: clicks, drag's, hovers or any sort of event that a user could trigger
38
38
and then have some behavior that happens in response
@@ -68,7 +68,7 @@ This object represents the entire page, this entire document https://developer.m
68
68
69
69
There's all of these different methods and properties available and they're all container within the **document object**. So the **document** is the entry point that we use to access the DOM, to manipulate things, to just view the content in the **DOM**, but also to add events add functionality, it's all located inside of this one object.
70
70
71
-
```
71
+
```javascript
72
72
// Run in the browser console
73
73
document.images
74
74
document.all
@@ -121,3 +121,34 @@ It is an array like object that is not an array, it's a type in the DOM and a ty
121
121
If we want to select based on CSS classes, we have a special method to do just that **getElementsByClassName**, **Elements** it's plural, this tells us that we'll get a collection back, we'll get multiple elements or we could get one element, but it will be in a collection just like **getElementsByTagName**.
122
122
123
123
If i try and select based on of an ID, it's not going to work.
124
+
125
+
## 9. querySelector & querySelectorAll
126
+
127
+
### querySelector
128
+
129
+
- A newer, all-in-one method to select **a single element**
130
+
- Pass in a CSS selector
131
+
132
+
```javascript
133
+
// Finds first h1 element:
134
+
document.querySelector('h1');
135
+
136
+
// Finds first element with ID of red:
137
+
document.querySelector('#red');
138
+
139
+
// Finds first element with class of
140
+
document.querySelector('.big');
141
+
```
142
+
143
+
**querySelector** it's like the swiss army knife of DOM selectors, it can select everything that we used. It can replicate the functionality of all of the selectors here.
144
+
145
+
We can select by an ID or a tag name or a class name or many other criteria.
146
+
147
+
**This only returns a single element at most**.
148
+
149
+
### querySelectorAll
150
+
Same idea, but returns **a collection** of matching elements
151
+
152
+
**querySelectorAll** which does the same thing except it returns a collection
153
+
154
+
A **NodeList** is returned with **querySelectorAll** and is another array like object
0 commit comments