--- id: javascript-dom-changing-html title: "JavaScript DOM Changing HTML" category: "Frontend" status: "draft" verification_status: "conceptual" canonical_id: "" aliases: ["innerHTML", "changing HTML content", "changing attributes", "document.write", "dynamic HTML content"] duplicate_of: "" source_trust_level: "B" confidence_score: 0.89 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_html.asp"] applied_in: [] github_commit: "" --- # [[JavaScript DOM Changing HTML]] ## 🎯 ν•œ 쀄 톡찰 (One-line insight) The HTML DOM lets JavaScript change the content of an element via `innerHTML` and change its attributes by assigning new values directly. [S1] ## 🧠 핡심 κ°œλ… (Core concepts) - **innerHTML changes content** β€” the easiest way to modify the content of an HTML element is with the `innerHTML` property. [S1] - **Assign to change attributes** β€” to change the value of an HTML attribute, assign a new value to the element's attribute property (e.g. `element.src = ...`). [S1] - **Dynamic content** β€” JavaScript can create HTML content dynamically, for example inserting the current date/time with `Date()`. [S1] - **document.write() is for generation, not editing** β€” it writes directly to the HTML output stream and must not be used after the document has loaded. [S1] ## 🧩 μΆ”μΆœλœ νŒ¨ν„΄ (Extracted patterns) - **`document.getElementById(id).innerHTML = new HTML`** β€” the canonical content-change syntax. [S1] - **`element.attribute = new value`** β€” the canonical attribute-change syntax. [S1] - **Guard against missing elements** β€” access DOM elements only after they exist. [S1] ## πŸ“– μ„ΈλΆ€ λ‚΄μš© (Details) **Changing HTML Content** [S1] The easiest way to modify the content of an HTML element is by using the `innerHTML` property. [S1] **The innerHTML Property** [S1] The syntax for changing the content of an HTML element is: [S1] ```javascript document.getElementById(id).innerHTML = new HTML ``` Change a paragraph's content: [S1] ```javascript document.getElementById("p1").innerHTML = "New text!"; ``` Change a heading's content: [S1] ```javascript const element = document.getElementById("id01"); element.innerHTML = "New Heading"; ``` **Common Mistakes** [S1] - Trying to access a DOM element before it exists. [S1] - Forgetting the quotes in an id like `"demo"`. [S1] **Changing an Attribute** [S1] To change the value of an HTML attribute, assign a new value to the attribute property β€” for example changing the `src` of an image: [S1] ```javascript document.getElementById("myImage").src = "landscape.jpg"; ``` **Dynamic HTML content** [S1] JavaScript can create dynamic HTML content, such as writing the current date and time with `Date()`. [S1] **document.write()** [S1] The `document.write()` method writes directly to the HTML output stream: [S1] ```javascript document.write(Date()); ``` **Warning!** [S1] > Never use `document.write()` after the document is loaded. It will overwrite the document. [S1] ## πŸ› οΈ 적용 사둀 (Applied in summary) The page's snippets β€” setting `#p1.innerHTML`, updating an `

` via `innerHTML`, swapping an image `src`, and `document.write(Date())` β€” are the canonical applied examples. No external project/commit applications found in the source. ## πŸ’» μ½”λ“œ νŒ¨ν„΄ (Code patterns) Change content (language: JavaScript): ```javascript document.getElementById("p1").innerHTML = "New text!"; ``` Change an attribute: ```javascript document.getElementById("myImage").src = "landscape.jpg"; ``` Write dynamic output (use only before the document finishes loading): ```javascript document.write(Date()); ``` ## βš–οΈ λͺ¨μˆœ 및 μ—…λ°μ΄νŠΈ (Contradictions & updates) No contradictions found in the source. (The source explicitly warns against post-load use of `document.write()`, which is a caveat rather than a contradiction.) [S1] ## βœ… 검증 μƒνƒœ 및 신뒰도 - **μƒνƒœ:** draft - **검증 단계:** conceptual (μ‹€μ œ 적용 사둀 발견 μ‹œ applied/validated둜 승격 κ°€λŠ₯) - **좜처 신뒰도:** B (W3Schools β€” widely used educational reference, not a primary standards body) - **μ‹ λ’° 점수:** 0.89 - **쀑볡 검사 κ²°κ³Ό:** μ‹ κ·œ 생성 (New discovery) ## πŸ”— 지식 κ·Έλž˜ν”„ (Knowledge Graph) - **μƒμœ„/루트:** [[JavaScript Tutorial]] - **κ΄€λ ¨ κ°œλ…:** [[JavaScript DOM Elements]], [[JavaScript DOM Changing CSS]], [[JavaScript HTML DOM]], [[JavaScript DOM Methods]] - **μ°Έμ‘° λ§₯락:** The content/attribute mutation step that follows selecting an element. ## πŸ“š 좜처 (Sources) - [S1] W3Schools β€” JavaScript DOM Changing HTML β€” https://www.w3schools.com/js/js_htmldom_html.asp ## πŸ“ λ³€κ²½ 이λ ₯ (Change history) - 2026-06-23: Initial draft synthesized from the W3Schools "JavaScript DOM Changing HTML" page (Astra wiki-curation, P-Reinforce v3.1 format).