-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocument.querySelector.js
More file actions
33 lines (19 loc) · 1.3 KB
/
document.querySelector.js
File metadata and controls
33 lines (19 loc) · 1.3 KB
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
// document.querySelector() is used to retrieve HTML elements from the DOM within JavaScript
// So, it allows you to get a DOM element with JavaScript, and then do something with that element like adding a class, changing its content, adding an event listener, etc.
// There are other ways to retrieve elements, but I think this is the most straightforward and flexible way for getting various elements
// How it works:
// You use the document object created by the browser, and then you access the querySelector method on it.
// The querySelector method takes one argument, a string, that's a CSS selector to decide what HTML element to retrieve.
// It will retrieve the first element it finds that matches the selector.
// e.g. document.querySelector('#some-id')
// However, you can use querySelectorAll() to retrieve a NodeList of all elements that match your selector.
// NOTE: a NodeList isn't an array you can iterate over it with forEach, but to do other iterations, you should convert it to an array first.
// e.g. document.querySelectorAll('li')
const title = document.querySelect("#title")
console.log(title)
title.style.color = 'red'
// Returns a NodeList
const listItems = document.querySelectorAll('li')
const arrList = Array.from(listItems)
const anotherList = [...listItems]
console.log(anotherList)