---
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 + "<br>";
}
```
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 + "<br>";
}
```

## ⚖️ 비교 및 선택 기준 (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).
