020
LVL 01 — WAVE DEFENDER SESSION 020 DAY 20

DOM & EVENTS

🎫 PIXELCRAFT-008 · Part 3 of 3
🐛 Bug (continued) | Priority: 🔴 Critical

Validation works now, but buttons STILL don't respond to clicks. The handler functions exist but they're never connected to the HTML buttons. Wire them up.
CONCEPTS.UNLOCKED
🌳
The DOM
How the browser turns HTML into JavaScript objects you can manipulate. Every tag becomes a node with properties and methods. The bridge between HTML and JS.
🔎
Selecting Elements
document.getElementById('id') — by id. document.querySelector('.class') — by CSS selector. document.querySelectorAll('selector') — all matches.
Modifying Elements
.textContent (change text), .innerHTML (change HTML), .classList.add/remove/toggle (CSS classes), .style (inline styles).
🖱
Events & Listeners
element.addEventListener('click', handler) — "when this element is clicked, run this function." The fundamental connection between user actions and code execution.
📋
Common Event Types
click, input, change, keydown, mousemove, load, submit. Each fires at a specific moment of user interaction.
📦
The Event Object
Every handler receives an event parameter: event.target (which element triggered it), event.type (what kind), event.preventDefault() (stop default behavior).
HANDS-ON.TASKS
01
Find the Root Cause

The addEventListener calls are missing/commented out in app.js. Handler functions exist but are never connected to buttons.

02
Select Elements
const uploadBtn = document.getElementById('upload-btn'); const grayscaleBtn = document.getElementById('grayscale-btn'); const canvas = document.getElementById('canvas'); console.log(uploadBtn); // The actual button element console.log(uploadBtn.textContent); // "Upload"
document.getElementById returns the actual DOM node — a JavaScript object with dozens of properties and methods. You can inspect it in the console.
03
Wire Buttons to Handlers
uploadBtn.addEventListener('click', function() { console.log("Upload button clicked!"); }); grayscaleBtn.addEventListener('click', function() { console.log("Grayscale button clicked!"); });

Wire ALL toolbar buttons to their respective handlers.

04
Make the Slider Interactive
const brightnessSlider = document.getElementById('brightness'); const brightnessValue = document.getElementById('brightness-value'); brightnessSlider.addEventListener('input', function(event) { brightnessValue.textContent = event.target.value; });

The 'input' event fires continuously as the slider moves. The 'change' event only fires when you release. We want real-time feedback → use 'input'.

05
Dynamic Text Updates

Change a button's text when clicked:

uploadBtn.addEventListener('click', function() { uploadBtn.textContent = "Uploading..."; // ... do upload ... uploadBtn.textContent = "Upload"; });
06
Add Keyboard Shortcuts
document.addEventListener('keydown', function(event) { if (event.ctrlKey && event.key === 'z') { event.preventDefault(); console.log("Undo!"); } });

event.preventDefault() stops the browser's default action (e.g., Ctrl+Z normally triggers the browser's undo, not yours).

07
Close the Ticket ✅
git switch -c bugfix/PIXELCRAFT-008-wire-events git add src/scripts/app.js git commit -m "Wire button events and fix upload validation (PIXELCRAFT-008)" git push origin bugfix/PIXELCRAFT-008-wire-events # PR → Review → Merge → Close ticket ✅
✅ PIXELCRAFT-008 — COMPLETE
Buttons respond to clicks. Upload validation works. Sliders update values. Keyboard shortcuts work. Three sessions. One ticket. Fully resolved.
CS.DEEP-DIVE

Event-driven programming.

Instead of running top-to-bottom and finishing, the program sets up listeners and waits. When something happens (click, keypress, network response), the registered handler runs.

// This model powers almost all modern software:

Web browsers    → click/scroll/type events
Mobile apps     → tap/swipe/shake events
Game engines    → game loop + event queue
Node.js         → handles requests as events
Operating systems → hardware interrupts
IoT devices     → sensor triggers

// Almost all modern software is event-driven.
// Understanding this model is understanding
// how interactive software works.
"Event Playground"
[A] Add a mousemove listener to the canvas area that shows the cursor's X,Y coordinates in real time. Display them in the corner of the canvas.
[B] Add keyboard shortcuts for all toolbar buttons: Ctrl+U (Upload), Ctrl+G (Grayscale), Ctrl+Z (Undo), Ctrl+Y (Redo), Ctrl+E (Export).
[C] Count how many events fire when you move the mouse across the canvas for 1 second (hint: increment a counter in the mousemove handler). The number will surprise you.
REF.MATERIAL
ARTICLE
Mozilla Developer Network
Complete guide to events: addEventListener, event objects, bubbling, capturing, and delegation. The reference for everything in this session.
EVENTSMDNESSENTIAL
VIDEO
Web Dev Simplified
Practical walkthrough: selecting elements, changing content, adding/removing classes, and handling events.
DOMPRACTICAL
ARTICLE
javascript.info
In-depth coverage of event types, handlers, the event object, and bubbling. Interactive examples you can run in the browser.
EVENTSINTERACTIVE
VIDEO
ColorCode
Visual explanation of how event listeners work, with clear diagrams of the event flow and practical button examples.
EVENTSVISUAL
ARTICLE
Mozilla Developer Network
The complete DOM API reference: nodes, elements, traversal, manipulation. The definitive resource for everything DOM.
DOMAPIREFERENCE
// LEAVE EXCITED BECAUSE
BUTTONS WORK! Click a button → something happens. Move a slider → the value updates. This is what makes a web page into a web APPLICATION. You connected human actions to code execution.