---
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).