--- id: javascript-dom-elements title: "JavaScript DOM Elements" category: "Frontend" status: "draft" verification_status: "conceptual" canonical_id: "" aliases: ["finding HTML elements", "getElementById", "getElementsByTagName", "getElementsByClassName", "querySelectorAll", "HTML object collections"] duplicate_of: "" source_trust_level: "B" confidence_score: 0.88 created_at: 2026-06-23 updated_at: 2026-06-23 review_reason: "" merge_history: [] tags: ["javascript", "js", "web", "frontend", "w3schools", "dom"] raw_sources: ["https://www.w3schools.com/js/js_htmldom_elements.asp"] applied_in: [] github_commit: "" --- # [[JavaScript DOM Elements]] ## 🎯 ν•œ 쀄 톡찰 (One-line insight) Before you can manipulate an HTML element with JavaScript, you must first find it β€” by id, tag name, class name, CSS selector, or via the built-in HTML object collections. [S1] ## 🧠 핡심 κ°œλ… (Core concepts) - **Find first, then manipulate** β€” often you want to manipulate HTML elements, and to do so you have to find the elements first. [S1] - **By id is the easiest** β€” the easiest way to find an HTML element in the DOM is by using the element id; it returns the element as an object, or `null` if not found. [S1] - **Collections by tag / class** β€” `getElementsByTagName()` finds all elements of a tag type; `getElementsByClassName()` finds all elements sharing a class name. [S1] - **CSS selectors** β€” `querySelector()` returns the first match for a CSS selector; `querySelectorAll()` returns all matches. [S1] - **HTML object collections** β€” predefined collections such as `document.forms` and `document.images` also give access to elements. [S1] ## 🧩 μΆ”μΆœλœ νŒ¨ν„΄ (Extracted patterns) - **One element vs many** β€” id and `querySelector()` return a single element; tag-name, class-name, and `querySelectorAll()` return collections you iterate over. [S1] - **Loop over a collection** β€” index into the returned collection (e.g. `x.elements[i].value`) to read each member. [S1] - **No `#` for getElementById** β€” pass the bare id, not a CSS-style selector. [S1] ## πŸ“– μ„ΈλΆ€ λ‚΄μš© (Details) **Finding HTML Elements** [S1] Often, with JavaScript, you want to manipulate HTML elements. To do so, you have to find the elements first. [S1] **Finding HTML Element by Id** [S1] The easiest way to find an HTML element in the DOM is by using the element id. It returns the element as an object if found, or `null` if not found. [S1] ```javascript const element = document.getElementById("intro"); ``` **Finding HTML Elements by Tag Name** [S1] ```javascript const element = document.getElementsByTagName("p"); ``` **Finding HTML Elements by Class Name** [S1] If you want to find all HTML elements with the same class name, use `getElementsByClassName()`. [S1] ```javascript const x = document.getElementsByClassName("intro"); ``` **Finding HTML Elements by CSS Selectors** [S1] If you want to find all HTML elements that match a specified CSS selector (id, class names, types, attributes, values of attributes, etc.), use the `querySelectorAll()` method. [S1] ```javascript const x = document.querySelectorAll("p.intro"); ``` **The querySelector() Method** β€” returns the first element that matches a specified CSS selector. [S1] **The querySelectorAll() Method** β€” returns all elements that match a specified CSS selector. [S1] **Finding HTML Elements by HTML Object Collections** [S1] The source also shows iterating over a collection (here a form's elements): [S1] ```javascript const x = document.forms["frm1"]; for (let i = 0; i < x.length; i++) { text += x.elements[i].value + "
"; } ``` Accessible HTML object collections include: `document.anchors`, `document.body`, `document.documentElement`, `document.embeds`, `document.forms`, `document.head`, `document.images`, `document.links`, `document.scripts`, and `document.title`. [S1] **Common Mistakes** [S1] - Using `#` inside `getElementById()` (wrong: `"#demo"`). [S1] - Forgetting that `querySelector()` returns only the first match. [S1] ## πŸ› οΈ 적용 사둀 (Applied in summary) The page's snippets β€” selecting by id, tag, class, and CSS selector, and looping a form's `elements` collection β€” are the canonical applied examples. No external project/commit applications found in the source. ## πŸ’» μ½”λ“œ νŒ¨ν„΄ (Code patterns) Four ways to find elements (language: JavaScript): ```javascript const byId = document.getElementById("intro"); const byTag = document.getElementsByTagName("p"); const byClass = document.getElementsByClassName("intro"); const bySelector = document.querySelectorAll("p.intro"); ``` Iterate a collection: ```javascript const x = document.forms["frm1"]; for (let i = 0; i < x.length; i++) { text += x.elements[i].value + "
"; } ``` ## βš–οΈ 비ꡐ 및 선택 κΈ°μ€€ (Comparison & decision criteria) - Use **`getElementById()`** when the element has a unique id β€” fastest, returns one object. [S1] - Use **`getElementsByTagName()` / `getElementsByClassName()`** to retrieve a live collection of multiple elements. [S1] - Use **`querySelector()`** for the first match of any CSS selector, and **`querySelectorAll()`** for all matches when you need full CSS-selector expressiveness. [S1] ## βš–οΈ λͺ¨μˆœ 및 μ—…λ°μ΄νŠΈ (Contradictions & updates) No contradictions found in the source. ## βœ… 검증 μƒνƒœ 및 신뒰도 - **μƒνƒœ:** draft - **검증 단계:** conceptual (μ‹€μ œ 적용 사둀 발견 μ‹œ applied/validated둜 승격 κ°€λŠ₯) - **좜처 신뒰도:** B (W3Schools β€” widely used educational reference, not a primary standards body) - **μ‹ λ’° 점수:** 0.88 - **쀑볡 검사 κ²°κ³Ό:** μ‹ κ·œ 생성 (New discovery) ## πŸ”— 지식 κ·Έλž˜ν”„ (Knowledge Graph) - **μƒμœ„/루트:** [[JavaScript Tutorial]] - **κ΄€λ ¨ κ°œλ…:** [[JavaScript HTML DOM]], [[JavaScript DOM Methods]], [[JavaScript DOM Changing HTML]], [[JavaScript DOM Changing CSS]] - **μ°Έμ‘° λ§₯락:** The element-selection step that precedes any content, style, or event manipulation. ## πŸ“š 좜처 (Sources) - [S1] W3Schools β€” JavaScript DOM Elements β€” https://www.w3schools.com/js/js_htmldom_elements.asp ## πŸ“ λ³€κ²½ 이λ ₯ (Change history) - 2026-06-23: Initial draft synthesized from the W3Schools "JavaScript DOM Elements" page (Astra wiki-curation, P-Reinforce v3.1 format).