--- id: javascript-html-events title: "JavaScript HTML Events" category: "Frontend" status: "draft" verification_status: "conceptual" canonical_id: "" aliases: ["HTML events", "onclick", "onload", "event handlers", "JavaScript events"] 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_events.asp"] applied_in: [] github_commit: "" --- # [[JavaScript HTML Events]] ## π― ν μ€ ν΅μ°° (One-line insight) HTML events are things that happen to HTML elements, and JavaScript event handlers are the code that runs when those events occur. [S1] ## π§ ν΅μ¬ κ°λ (Core concepts) - **An event handler is code that runs on an event** β an event handler is JavaScript code that runs when an event happens. [S1] - **Events can call a function** β an event attribute can run JavaScript inline or call a named function. [S1] - **`this` refers to the element** β inside an inline handler, `this` refers to the element that received the event. [S1] - **addEventListener is the recommended approach** β attaching handlers with `addEventListener()` keeps JavaScript separate from the HTML markup. [S1] ## π§© μΆμΆλ ν¨ν΄ (Extracted patterns) - **Inline handler** β `onclick="..."` runs JavaScript directly in the HTML attribute. [S1] - **Function call handler** β `onclick="displayDate()"` delegates to a named function. [S1] - **Listener handler** β `element.addEventListener("click", fn)` attaches behavior from script. [S1] ## π μΈλΆ λ΄μ© (Details) **HTML Events** [S1] An HTML event can be something the browser does, or something a user does. JavaScript lets you execute code when events are detected. [S1] **JavaScript Events / Calling a JavaScript Function** [S1] A basic `onclick` handler running JavaScript inline: [S1] ```html ``` Using `this` to refer to the element itself: [S1] ```html ``` Calling a named function from the handler: [S1] ```html ``` **Common HTML Events** [S1] | Event | Description | |-------|-------------| | `onchange` | An HTML element has been changed | | `onclick` | The user clicks an HTML element | | `onmouseover` | The user moves the mouse over an HTML element | | `onmouseout` | The user moves the mouse away from an HTML element | | `onkeydown` | The user pushes a keyboard key | | `onload` | The browser has finished loading the page | **JavaScript Event Handlers** [S1] An event handler is JavaScript code that runs when an event happens. [S1] **Using an Event Listener** [S1] The recommended way to attach a handler from script is `addEventListener()`: [S1] ```html
``` ## π οΈ μ μ© μ¬λ‘ (Applied in summary) The page's snippets β `onclick` writing `Date()`, `this.innerHTML`, a `displayDate()` function, and an `addEventListener("click", ...)` handler β are the canonical applied examples. No external project/commit applications found in the source. ## π» μ½λ ν¨ν΄ (Code patterns) Inline handler (language: HTML): ```html ``` Function-call handler (language: HTML/JavaScript): ```html ``` Event listener (language: JavaScript): ```javascript const btn = document.getElementById("myBtn"); btn.addEventListener("click", function () { document.getElementById("demo").innerHTML = Date(); }); ``` ## βοΈ λΉκ΅ λ° μ ν κΈ°μ€ (Comparison & decision criteria) - Use **inline `on*` attributes** for quick, simple handlers written directly in the HTML. [S1] - Use **`addEventListener()`** when you want the JavaScript separated from the HTML markup for better readability and to attach multiple handlers. [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 DOM Event Listener]], [[JavaScript DOM Changing CSS]], [[JavaScript DOM Changing HTML]], [[JavaScript HTML DOM]] - **μ°Έμ‘° λ§₯λ½:** The event layer that triggers DOM content and style changes in response to user/browser actions. ## π μΆμ² (Sources) - [S1] W3Schools β JavaScript HTML Events β https://www.w3schools.com/js/js_events.asp ## π λ³κ²½ μ΄λ ₯ (Change history) - 2026-06-23: Initial draft synthesized from the W3Schools "JavaScript HTML Events" page (Astra wiki-curation, P-Reinforce v3.1 format).