--- 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 `