JavaScript - The Complete Guide 2022

Section 7: Interacting with the HTML Page - Exploring the DOM

olivia_yj 2022. 10. 17. 21:47

The goals

๐Ÿ’ช๐ŸปHTML, DOM & JavaScript

โœŒ๐ŸปNodes & Elements

๐Ÿ‘๐ŸปQuerying DOM Nodes & Traversing the DOM

๐Ÿ‘๐ŸปEvaluating & Manipulating DOM Nodes

๐Ÿ‘Š๐ŸปCreating & Removing DOM Nodes

 

 

The Document Object Model (DOM)

Node Query Methods

๋‹ค์Œ์€ DOM ์š”์†Œ์— ์ ‘๊ทผํ•˜๋Š” ๋‹ค์–‘ํ•œ ๋ฐฉ๋ฒ•์— ๋Œ€ํ•œ ์š”์•ฝ์ž…๋‹ˆ๋‹ค (์ฐธ๊ณ : ์š”์†Œ ๋…ธ๋“œ์— ๋Œ€ํ•ด์„œ๋งŒ ์ฟผ๋ฆฌํ•  ์ˆ˜ ์žˆ์Œ).

์•„๋ž˜์˜ ์ฟผ๋ฆฌ ๋ฉ”์„œ๋“œ ์™ธ์—๋„ ๋ฌธ์„œ ๊ฐ์ฒด์— ๋Œ€ํ•œ ์•„๋ž˜์˜ ํŠน์ˆ˜ ํ”„๋กœํผํ‹ฐ๋กœ ๋ฌธ์„œ์˜ ์ผ๋ถ€๋ฅผ ์„ ํƒํ•ฉ๋‹ˆ๋‹ค.

document.body =>  <body> ์š”์†Œ ๋…ธ๋“œ๋ฅผ ์„ ํƒํ•ฉ๋‹ˆ๋‹ค.

document.head => <head> ์š”์†Œ ๋…ธ๋“œ๋ฅผ ์„ ํƒํ•ฉ๋‹ˆ๋‹ค.

document.documentElement =>  <html> ์š”์†Œ ๋…ธ๋“œ๋ฅผ ์„ ํƒํ•ฉ๋‹ˆ๋‹ค.

---

์ฟผ๋ฆฌ ๋ฉ”์„œ๋“œ

---

document.querySelector(<CSS selector>);

๋ชจ๋“  CSS ์„ ํƒ์ž(์˜ˆ: '#id์ด๋ฆ„', '.ํด๋ž˜์Šค๋ช…' ๋˜๋Š” 'div p.ํด๋ž˜์Šค๋ช…')๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ DOM์—์„œ ์ฒ˜์Œ(!) ์ผ์น˜ํ•˜๋Š” ์š”์†Œ๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ์ผ์น˜ํ•˜๋Š” ์š”์†Œ๋ฅผ ์ฐพ์„ ์ˆ˜ ์—†๋Š” ๊ฒฝ์šฐ์—๋Š” null ์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

์ถ”๊ฐ€ ์ •๋ณด:  https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelector

 

document.getElementById(<ID>);

 ID(# ์—†์ด ID ์ด๋ฆ„๋งŒ)๋ฅผ ๊ฐ€์ ธ์™€ ์ด ID๋ฅผ ๊ฐ€์ง„ ์š”์†Œ๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ๋™์ผํ•œ ID๊ฐ€ ํŽ˜์ด์ง€์—์„œ ๋‘ ๋ฒˆ ์ด์ƒ ๋ฐœ์ƒํ•˜์ง€ ์•Š์œผ๋ฏ€๋กœ ํ•ญ์ƒ ์ •ํ™•ํžˆ ํ•˜๋‚˜์˜ ์š”์†Œ๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ์ง€์ •๋œ ID๋ฅผ ๊ฐ€์ง„ ์š”์†Œ๋ฅผ ์ฐพ์„ ์ˆ˜ ์—†๋Š” ๊ฒฝ์šฐ null ์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

์ถ”๊ฐ€ ์ •๋ณด:  https://developer.mozilla.org/en-US/docs/Web/API/Document/getElementById

document.querySelectorAll(<CSS selector>);

๋ชจ๋“  CSS ์„ ํƒ์ž(์˜ˆ: '#id์ด๋ฆ„', '.ํด๋ž˜์Šค๋ช…' ๋˜๋Š” 'div p.ํด๋ž˜์Šค๋ช…')๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ DOM์—์„œ ์ผ์น˜ํ•˜๋Š” ๋ชจ๋“  ์š”์†Œ๋ฅผ ์ •์ (๋„Œ ๋ผ์ด๋ธŒ) NodeList๋กœ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ์ผ์น˜ํ•˜๋Š” ์š”์†Œ๋ฅผ ์ฐพ์„ ์ˆ˜ ์—†์œผ๋ฉด ๋นˆ NodeList ๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

์ถ”๊ฐ€ ์ •๋ณด:  https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelectorAll

document.getElementsByClassName(<CSS CLASS>);

CSS ํด๋ž˜์Šค g(์˜ˆ: 'ํด๋ž˜์Šค๋ช…')๋ฅผ ๊ฐ€์ ธ์™€ DOM์—์„œ ์ผ์น˜ํ•˜๋Š” ์š”์†Œ์˜ ๋ผ์ด๋ธŒ HTMLCollection ์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ์ผ์น˜ํ•˜๋Š” ์š”์†Œ๋ฅผ ์ฐพ์„ ์ˆ˜ ์—†์œผ๋ฉด ๋นˆ HTMLCollection์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

์ถ”๊ฐ€ ์ •๋ณด:  https://developer.mozilla.org/en-US/docs/Web/API/Document/getElementsByClassName

document.getElementsByTagName(<HTML TAG>);

HTML ํƒœ๊ทธ(์˜ˆ: 'p')๋ฅผ ๊ฐ€์ ธ์™€ DOM์—์„œ ์ผ์น˜ํ•˜๋Š” ์š”์†Œ์˜ ๋ผ์ด๋ธŒ HTMLCollection์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ์ผ์น˜ํ•˜๋Š” ์š”์†Œ๋ฅผ ์ฐพ์„ ์ˆ˜ ์—†์œผ๋ฉด ๋นˆ HTMLCollection์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

์ถ”๊ฐ€ ์ •๋ณด: https://developer.mozilla.org/en-US/docs/Web/API/Element/getElementsByTagName

์ผ๋ฐ˜์ ์œผ๋กœ๋Š” ์‚ฌ์šฉ๋˜์ง€ ์•Š๋Š” getElementsByName() ๋ฉ”์„œ๋“œ๋„ ์žˆ์Šต๋‹ˆ๋‹ค(https://developer.mozilla.org/en-US/docs/Web/API/Document/getElementsByName<2 }).

 

 Quiz

์งˆ๋ฌธ 1:

DOM์€ ์–ด๋””์—์„œ “์ฐพ์„” ์ˆ˜ ์žˆ์„๊นŒ์š”?

๋‹ต: ๋ธŒ๋ผ์šฐ์ €์— ์˜ํ•ด JavaScript์— ๋…ธ์ถœ๋˜๋Š” API์˜ ์ผ๋ถ€์ž…๋‹ˆ๋‹ค.

JavaScript๋Š” "ํ˜ธ์ŠคํŒ…๋œ ์–ธ์–ด"๋ผ๋Š” ์ ์„ ๊ธฐ์–ตํ•˜์„ธ์š”. ํ˜ธ์ŠคํŠธ ํ™˜๊ฒฝ์œผ๋กœ์„œ์˜ ๋ธŒ๋ผ์šฐ์ €๋Š” ์ด DOM API๋ฅผ JS ์ฝ”๋“œ์— ์ž๋™์œผ๋กœ ๋…ธ์ถœํ•ฉ๋‹ˆ๋‹ค. 

 

์งˆ๋ฌธ 3:

document.querySelector('#Id ์ด๋ฆ„')์™€ document.getElementById('Id ์ด๋ฆ„')์˜ ์ฐจ์ด์ ์€ ๋ฌด์—‡์ผ๊นŒ์š”?

๋‹ต: querySelector๋Š” CSS ์„ ํƒ์ž๋กœ (์ œ๊ณต๋œ ์„ ํƒ์ž์— ๋”ฐ๋ผ) ๋ชจ๋“  ์š”์†Œ์™€ ์ผ์น˜ํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ getElementById๋Š” ID๋กœ๋งŒ ์ฐพ์Šต๋‹ˆ๋‹ค.

 

 

์งˆ๋ฌธ 4:

querySelector()์™€ querySelectorAll()์˜ ์ฐจ์ด์ ์€ ๋ฌด์—‡์ผ๊นŒ์š”?

๋‹ต: querySelector๋Š” ์ฒ˜์Œ ์ผ์น˜ํ•˜๋Š” ์š”์†Œ๋ฅผ ์ฐพ๊ณ  querySelectorAll์€ ์ผ์น˜ํ•˜๋Š” ๋ชจ๋“  ์š”์†Œ๋ฅผ ์ฐพ์•„ ๋ฐฐ์—ด๊ณผ ์œ ์‚ฌํ•œ ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. 

 

์š”์•ฝ: ์‚ฝ์ž…, ๋Œ€์ฒด, ์ œ๊ฑฐ

DOM ์š”์†Œ๋ฅผ ์ƒ์„ฑ, ์‚ฝ์ž…, ๊ต์ฒด, ์ œ๊ฑฐํ•˜๋Š” ๋ฐฉ๋ฒ•์—๋Š” ์—ฌ๋Ÿฌ ๊ฐ€์ง€๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹ค์Œ์€ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ ๋ฐฉ๋ฒ•์— ๋Œ€ํ•œ ์š”์•ฝ์ž…๋‹ˆ๋‹ค.

๋ธŒ๋ผ์šฐ์ € ์ง€์›์— ๋Œ€ํ•ด์„œ๋Š” ์ œ๊ณต๋œ ๋งํฌ์™€ ๊ฐ•์˜ ํ›„๋ฐ˜๋ถ€์— ์žˆ๋Š” "๋ธŒ๋ผ์šฐ์ € ์ง€์›" ๋ชจ๋“ˆ์„ ํ™•์ธํ•˜์‹ญ์‹œ์˜ค.

์ƒ์„ฑ & ์‚ฝ์ž…

ํฌ๊ฒŒ ๋‘ ๊ฐ€์ง€ ๋ฐฉ๋ฒ•์ด ์žˆ์Šต๋‹ˆ๋‹ค. HTML ์Šค๋‹ˆํŽซ(์˜ˆ: innerHTML์„ ํ†ตํ•ด)์„ ์œ ํšจํ•œ HTML ์Šค๋‹ˆํŽซ์— ์ œ๊ณตํ•˜๊ณ  ๋ธŒ๋ผ์šฐ์ €๊ฐ€ ์ด๋ฅผ ๋ Œ๋”๋งํ•˜๋„๋ก ํ•˜๊ฑฐ๋‚˜ JS ์ฝ”๋“œ๋กœ DOM ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•œ ํ›„ ์ˆ˜๋™์œผ๋กœ ์ถ”๊ฐ€/์‚ฝ์ž…ํ•ฉ๋‹ˆ๋‹ค. ํ›„์ž์˜ ์ ‘๊ทผ ๋ฐฉ์‹์€ DOM ๊ฐ์ฒด์— ์ง์ ‘ ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ์žฅ์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค(ํ”„๋กœํผํ‹ฐ๋ฅผ ์„ค์ •ํ•˜๊ฑฐ๋‚˜ ์ด๋ฒคํŠธ ๋ฆฌ์Šค๋„ˆ๋ฅผ ์ถ”๊ฐ€ํ•˜๋Š” ๋ฐ ์œ ์šฉ). ๋‹จ์ ์€ ๋” ๋งŽ์€ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•ด์•ผ ํ•œ๋‹ค๋Š” ๊ฒ๋‹ˆ๋‹ค.

HTML ์ฝ”๋“œ ์ถ”๊ฐ€ํ•˜๊ธฐ.

 

const root = document.getElementById('root-el'); // selects something like <div id="root-el">
root.innerHTML = `
<div>
<h2>Welcome!</h2>
<p>This is all create & rendered automatically!</p>
</div>
`;

 

์ค‘์š”ํ•œ ์ฐธ๊ณ  ์‚ฌํ•ญ innerHTML์„ ์‚ฌ์šฉํ•˜๋ฉด root์— ์žˆ๋˜ ๋‚ด์šฉ์ด ์™„์ „ํžˆ ๋Œ€์ฒด๋ฉ๋‹ˆ๋‹ค. HTML ์ฝ”๋“œ๋ฅผ ์ถ”๊ฐ€/์‚ฝ์ž…ํ•˜๋ ค๋ฉด insertAdjacentHTML์„ ๋Œ€์‹  ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. https://developer.mozilla.org/en-US/docs/Web/API/Element/insertAdjacentHTML

const root = document.getElementById('root-el'); // selects something like <div id="root-el">
root.insertAdjacentHTML('afterbegin', `
<div>
<h2>Welcome!</h2>
<p>This is all create & rendered automatically!</p>
</div>
`);

DOM ๊ฐ์ฒด๋ฅผ ์ˆ˜๋™์œผ๋กœ ์ƒ์„ฑ, ์‚ฝ์ž…ํ•˜๊ธฐ.

const someParagraph = document.createElement('p'); // creates a "p" element (i.e. a <p> element)
const root = document.getElementById('root-el'); // selects something like <div id="root-el">
root.append(someParagraph);

์ด ์˜ˆ์—์„œ๋Š” ๋‹จ๋ฝ์„ ๋งŒ๋“ค์–ด root์— ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค - ์ด๋Š” root์˜ ๋์— ์‚ฝ์ž…๋จ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค(์ฆ‰ root ๋‚ด๋ถ€์— ์žˆ์ง€๋งŒ ๋‹ค๋ฅธ ๋ชจ๋“  ํ•˜์œ„ ๋…ธ๋“œ ์ดํ›„์— ์ถ”๊ฐ€๋จ).

์‚ฝ์ž…ํ•˜๋Š” ๋ฉ”์„œ๋“œ:

append() => https://developer.mozilla.org/en-US/docs/Web/API/ParentNode/append

๋ธŒ๋ผ์šฐ์ € ์ง€์›์€ ๊ดœ์ฐฎ๊ฒŒ ๋˜๋Š” ํŽธ์ด์ง€๋งŒ IE์—์„œ๋Š” appendChild()์ด ์„ ํ˜ธ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค =>  https://developer.mozilla.org/en-US/docs/Web/API/Node/appendChild

prepend() => https://developer.mozilla.org/en-US/docs/Web/API/ParentNode/prepend

๋ธŒ๋ผ์šฐ์ € ์ง€์›์€ ๊ดœ์ฐฎ๊ฒŒ ๋˜๋Š” ํŽธ์ด์ง€๋งŒ IE์—์„œ๋Š” insertBefore()์ด ์„ ํ˜ธ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค => https://developer.mozilla.org/en-US/docs/Web/API/Node/insertBefore

before(), after() => https://developer.mozilla.org/en-US/docs/Web/API/ChildNode/before https://developer.mozilla.org/en-US/docs/Web/API/ChildNode/after

๋ธŒ๋ผ์šฐ์ €๋ฅผ ์ง€์›ํ•˜์ง€๋งŒ IE์™€ Safari๋Š” ์ง€์›ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. insertBefore() (https://developer.mozilla.org/en-US/docs/Web/API/Node/insertBefore) or insertAdjacentElement() (https://developer.mozilla.org/en-US/docs/Web/API/Element/insertAdjacentElement) as substitutes.

์ค‘์š”ํ•œ ์ฐธ๊ณ  ์‚ฌํ•ญ(์š”์†Œ๋ฅผ ์‚ฝ์ž…ํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๊ด€๊ณ„์—†์ด). ์š”์†Œ๋ฅผ ์‚ฝ์ž…ํ•  ๋•Œ ์ด์ „์— ์ด๋ฏธ ์‚ฝ์ž…ํ•œ ๊ฒฝ์šฐ ์ƒˆ ์œ„์น˜๋กœ ์š”์†Œ๋ฅผ ์ด๋™ํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๋ณต์‚ฌ๋˜๋Š” ๊ฒƒ์ด์•„๋‹™๋‹ˆ๋‹ค(someElement.cloneNode(true)๋กœ ์š”์†Œ๋ฅผ ๋ณต์‚ฌํ•  ์ˆ˜ ์žˆ๊ธด ํ•ฉ๋‹ˆ๋‹ค).

๋Œ€์ฒด

DOM์˜ ์š”์†Œ๋ฅผ ์•„๋ž˜์˜ ๋‘ ๊ฐ€์ง€ ๋ฐฉ๋ฒ•์œผ๋กœ ๊ต์ฒดํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

replaceWith() => https://developer.mozilla.org/en-US/docs/Web/API/ChildNode/replaceWith

replaceChild() => https://developer.mozilla.org/en-US/docs/Web/API/Node/replaceChild

replaceWith()๋Š” ์กฐ๊ธˆ ๋” ์‰ฝ๊ฒŒ ์“ธ ์ˆ˜ ์žˆ๊ณ  ๋ธŒ๋ผ์šฐ์ € ์ง€์›๋„ ์ ์ ˆํ•ฉ๋‹ˆ๋‹ค. ๋‹จ, IE๋Š” ์˜ˆ์™ธ์ž…๋‹ˆ๋‹ค. IE๋ฅผ ์‚ฌ์šฉํ•˜์‹ ๋‹ค๋ฉด replaceChild()๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์„ ๊ณ ๋ คํ•ด ๋ณด์„ธ์š”.

์ œ๊ฑฐ

์•„๋ž˜์˜ ์„ธ ๊ฐ€์ง€ ๋ฐฉ๋ฒ•์œผ๋กœ ์š”์†Œ๋ฅผ ์ œ๊ฑฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค:

someElement.innerHTML = ''=> someElement์˜ ๋ชจ๋“  HTML ๋‚ด์šฉ์„ ์ง€์›Œ ๋ Œ๋”๋ง๋œ ๋ชจ๋“  ๊ฐ์ฒด๋ฅผ ์ œ๊ฑฐํ•ฉ๋‹ˆ๋‹ค.

someElement.remove()=> DOM์—์„œ ๋‹จ์ผ ์š”์†Œ(someElement)๋ฅผ ์ œ๊ฑฐ(https://developer.mozilla.org/en-US/docs/Web/API/ChildNode/remove).

๋ธŒ๋ผ์šฐ์ € ์ง€์›์€ ํ›Œ๋ฅญํ•˜์ง€๋งŒ IE๋Š” ์•„๋‹™๋‹ˆ๋‹ค. ๋Œ€์‹  removeChild(๋‹ค์Œ์„ ์ฐธ๊ณ )๋ฅผ ์‚ฌ์šฉํ•˜์„ธ์š”.

someElement.parentNode.removeChild(someElement) => (ํ˜ธ์ถœํ•˜๋Š” ์š”์†Œ๊ฐ€ ์•„๋‹Œ) ์ œ๊ณต๋œ ์ž์‹ ์š”์†Œ๋ฅผ ์ œ๊ฑฐํ•ฉ๋‹ˆ๋‹ค. ๊ด‘๋ฒ”์œ„ํ•œ ๋ธŒ๋ผ์šฐ์ € ์ง€์›์„ ์ œ๊ณตํ•˜์ง€๋งŒ ๋” ๋งŽ์€ ์ฝ”๋“œ๊ฐ€ ์ž‘์„ฑ๋˜์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค(https://developer.mozilla.org/en-US/docs/Web/API/Node/removeChild).

ํ…์ŠคํŠธ ๋…ธ๋“œ๋Š” ์–ด๋–จ๊นŒ์š”?

ํ•œ ๋ฒˆ์— ํ…์ŠคํŠธ ๋…ธ๋“œ๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ์‚ฝ์ž…ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

someElement.textContent = 'Hi there!';

์œ„์˜ ์ฝ”๋“œ๋Š” 'Hi there!' ๋‚ด์šฉ์˜ ํ…์ŠคํŠธ ๋…ธ๋“œ๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ์‚ฝ์ž…ํ•ฉ๋‹ˆ๋‹ค.

๊ธฐ์กด์— ์žˆ๋Š” ํ…์ŠคํŠธ๋ฅผ ์ถ”๊ฐ€ํ•˜๊ณ  ์‹ถ์œผ์‹ ๊ฐ€์š”?

์•„๋ž˜์˜ ์ฝ”๋“œ๋ฅผ ์“ฐ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

someElement.textContent = someElement.textContent + 'More text!';

 

 

 

 

 

Sources

 

Element.insertAdjacentHTML() - Web APIs | MDN

The insertAdjacentHTML() method of the Element interface parses the specified text as HTML or XML and inserts the resulting nodes into the DOM tree at a specified position.

developer.mozilla.org

 

Element.insertAdjacentHTML() - Web APIs | MDN

The insertAdjacentHTML() method of the Element interface parses the specified text as HTML or XML and inserts the resulting nodes into the DOM tree at a specified position.

developer.mozilla.org

 

Element.insertAdjacentHTML() - Web APIs | MDN

The insertAdjacentHTML() method of the Element interface parses the specified text as HTML or XML and inserts the resulting nodes into the DOM tree at a specified position.

developer.mozilla.org

 

Document.getElementById() - Web APIs | MDN

The Document method getElementById() returns an Element object representing the element whose id property matches the specified string. Since element IDs are required to be unique if specified, they're a useful way to get access to a specific element quick

developer.mozilla.org

 

Document.getElementById() - Web APIs | MDN

The Document method getElementById() returns an Element object representing the element whose id property matches the specified string. Since element IDs are required to be unique if specified, they're a useful way to get access to a specific element quick

developer.mozilla.org

 

Document.getElementById() - Web APIs | MDN

The Document method getElementById() returns an Element object representing the element whose id property matches the specified string. Since element IDs are required to be unique if specified, they're a useful way to get access to a specific element quick

developer.mozilla.org

 

Document.querySelector() - Web APIs | MDN

The Document method querySelector() returns the first Element within the document that matches the specified selector, or group of selectors. If no matches are found, null is returned.

developer.mozilla.org