Query DOM JavaScript, Schemes and Mind Maps of Computer Communication Systems

A Front-End Developer is someone who creates websites and web applications. The difference between Front-End and Back-End is that Front-End refers to how a web page looks, while back-end refers to how it works. You can think of Front-End as client-side and Back-End as server-side.

Typology: Schemes and Mind Maps

2022/2023

Uploaded on 06/06/2023

hung-do-5
hung-do-5 🇻🇳

1 / 5

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Javascript cho người mới bắt đầu 2021
🎉
Easy Frontend © 2021
1 / 5
Q
u
e
ry
DOM
-
H
ow
to
fi
n
d
a
n
ele
m
e
nt
?
AGENDA
.
F
i
n
d
a
n
ele
m
e
nt
.
F
i
n
d
all
ele
m
e
nts
1.
F
i
n
d
a
n
ele
m
e
nt
#
N
a
m
e
D
e
s
c
1
id
-
n
a
m
ed
gl
o
bal
v
a
r
iable
s support
ed
m
ai
n
l
y
f
or
c
omp
a
t
ibili
ty
-->
a
vo
id
us
i
n
g
t
hi
s
1
d
o
c
um
e
nt
.
ge
t
E
le
m
e
nt
B
y
I
d
(
id
)
(
not
a
v
ailable
on
E
le
m
e
nt
)
r
e
turn
nu
ll
if
not
f
oun
d
,
r
e
turn
t
he
ele
m
e
nt
ha
v
i
n
g
id
2
d
o
c
um
e
nt
.
qu
e
ry
S
elec
tor
(
s
elec
tor
)
r
e
turn
t
he
fi
rst
E
le
m
e
nt
m
a
t
ch
CSS
s
elec
tor
,
us
i
n
g
DFS P
r
e
-
or
de
r
,
r
e
turn
nu
ll
ot
he
rw
i
s
e
<h1 id="title">Easy Frontend</h1>
const title = document.getElementById('title');
if (title) {
console.log(title.textContent)
}
// remember to have hash before the id (css selector)
const title = document.querySelector('#title');
if (title) {
console.log(title.textContent)
}
ge
t
E
le
m
e
nt
B
y
I
d
qu
e
ry
S
elec
tor
f
or
d
o
c
um
e
nt
on
l
y
Y
ES NO
,
al
so
a
v
ailable
on
E
le
m
e
nt
s
ea
r
ch
b
y
id c
ss
s
elec
tor
r
e
turn
ele
m
e
nt
ha
v
i
n
g
m
a
t
ch
id
t
he
fi
rst
no
de
m
a
t
che
s
s
elec
tor
w
he
n
to
us
e
qu
e
ry
b
y
id
qu
e
ry
b
y
ot
he
rs
pf3
pf4
pf5

Partial preview of the text

Download Query DOM JavaScript and more Schemes and Mind Maps Computer Communication Systems in PDF only on Docsity!

Query DOM - How to find an element?

AGENDA

. Find an element . Find all elements

1. Find an element

Name Desc

1 id-named global variables supported mainly for compatibility --> avoid using this

document.getElementById(id) (not available on Element)

return null if not found, return the element having id

2 document.querySelector(selector)

return the first Element match CSS selector, using DFS Pre- order, return null otherwise

Easy Frontend

const title = document.getElementById('title'); if (title) { console.log(title.textContent) }

// remember to have hash before the id (css selector) const title = document.querySelector('#title'); if (title) { console.log(title.textContent) }

getElementById querySelector

for document only YES NO, also available on Element

search by id css selector

return element having match id the first node matches selector

when to use query by id query by others

REMEMBER: Narrow down the scope when querying

Javascript cho người mới bắt đầu 🎉

39.99$

// DON'T

const productName = document.querySelector('#product-123 > .product__name');

const productPrice = document.querySelector('#product-123 > .product__price'); // as it need to search on the whole tree over and over again for each query

// DO: arrow down the query scope // 1. Get the product first // 2. From the product element, search the element inside it's subtree only const product = document.getElementById('product-123'); if (product) { const productName = product.querySelector('.product__name'); const productPrice = product.querySelector('.product__name'); }

Can use multiple CSS selectors by commas

  • Learn Javascript
  • Learn ReactJS
  • Learn NextJS
  • Learn Javascript
  • Learn ReactJS
  • Learn NextJS

// 1. Narrow down the scope of querying const todoListElement = document.getElementById('todoList');

// 2. Make sure to check if existed before using if (todoListElement) { // 3. Some ways of query all li todoListElement.querySelectorAll('li'); todoListElement.querySelectorAll('ul > li'); todoListElement.querySelectorAll('ul.todo-list > li');

todoListElement.querySelectorAll( 'ul[data-weekday="monday"] > li, ul[data-weekday="tuesday"] > li' ); }

Source: https://javascript.info/searching-elements-dom

📝 Một vài lưu ý

DOM càng lớn, search càng tốn thời gian. Hãy thu hẹp phạm vi search khi có thể. getElementById(): query by id querySelector(): query by css selector querySelectorAll(): query all by css selector

Tham khảo

https://developer.mozilla.org/en-US/docs/Web/API/Document/getElementById https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelector https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelectorAll https://javascript.info/searching-elements-dom

Khoá học Javascript cho người mới bắt đầu 2021 🎉

Tác giả: Hậu Nguyễn - Founder Easy Frontend Khoá học chỉ được published trên Udemy, không thông qua trung gian. Khoá học không bán dạng videos upload trên Google Drive hay bất cứ hình thức nào tương tự. Khoá học có nhóm discord để hỗ trợ trong quá trình học tập.

☎ Liên hệ tác giả để được hỗ trợ:

✅ Facebook: https://www.facebook.com/nvhauesmn/ ✅ Fanpage: https://www.facebook.com/learn.easyfrontend ✅ Youtube Channel: https://www.youtube.com/easyfrontend