JavaScript Curriculum
The Page Is a Tree
mediumEverything you've learned so far has run in isolation — no page, no user, no interaction. Now connect it to the browser. The Nexus dashboard is a live HTML document. When a user clicks 'View Profile', JavaScript needs to find the right element, fetch the data, and update the page — without a reload. This is the DOM.
What is the DOM?
When a browser loads an HTML file, it doesn't store it as text. It parses the markup and builds a live tree of JavaScript objects — one object per element, with every attribute, every style, and every piece of text accessible as a property. This tree is the Document Object Model — the DOM.
Every node in that tree is a live JavaScript object. Change the object — the page changes instantly. No reload. No server round-trip.
Selecting elements
Before you can do anything to an element, you need a reference to it. JavaScript gives you several ways to get one.
Click any element in the tree below to see the exact code that selects it:
Click any element in the tree to see how to select it with JavaScript.
Reading and writing element properties
Once you have a reference, you can read and change almost anything:
Try the sandbox below — each action shows the exact JavaScript that produced it:
live element
Nexus Dashboard
Senior Engineer
actions
appendChild() — add a badge
classList — the right way to manage styles
Toggling inline styles directly is fragile. The idiomatic approach: keep visual states in CSS classes and use classList to apply them:
Your CSS defines what .highlighted looks like. JavaScript just switches it on and off. This separation keeps your code clean and your styles reusable.
Creating and inserting elements
You can build new DOM nodes entirely in JavaScript and insert them anywhere:
Events — responding to user interaction
The DOM broadcasts events when things happen: clicks, keypresses, form submissions, mouse moves, page loads. You attach a listener function that runs when the event fires.
Event bubbling and capturing
When an event fires on an element, it doesn't stay there. It travels — first down the tree (capturing phase, rarely used), then back up (bubbling phase). Every ancestor that has a listener for that event type will have its handler called.
Click the button and watch the event travel upward through each ancestor:
DOM structure
event listener log
click the button…
handler code
Without stopPropagation, the event bubbles upward through every ancestor that has a listener.
Removing event listeners
Listeners attached with addEventListener stay active until explicitly removed or the element is destroyed:
Your challenge
setupCard(name, role) is pure DOM wiring. Select two elements by their selectors, set their text content, then attach a click listener to the button. The listener logs a message that uses the name parameter — which means it needs to be a closure over the function argument.
That last part is important: the listener closes over name and will still have access to it when the button is clicked later.
Challenge
Write a function called setupCard(name, role) that: (1) queries the element with id 'user-name' and sets its textContent to name, (2) queries the element with class 'user-role' and sets its textContent to role, (3) queries the button with id 'profile-btn' and adds a click event listener that logs 'Profile opened for: ' + name. Call setupCard('Jordan', 'Security Lead').
The Page Is a Tree
mediumEverything you've learned so far has run in isolation — no page, no user, no interaction. Now connect it to the browser. The Nexus dashboard is a live HTML document. When a user clicks 'View Profile', JavaScript needs to find the right element, fetch the data, and update the page — without a reload. This is the DOM.
What is the DOM?
When a browser loads an HTML file, it doesn't store it as text. It parses the markup and builds a live tree of JavaScript objects — one object per element, with every attribute, every style, and every piece of text accessible as a property. This tree is the Document Object Model — the DOM.
Every node in that tree is a live JavaScript object. Change the object — the page changes instantly. No reload. No server round-trip.
Selecting elements
Before you can do anything to an element, you need a reference to it. JavaScript gives you several ways to get one.
Click any element in the tree below to see the exact code that selects it:
Click any element in the tree to see how to select it with JavaScript.
Reading and writing element properties
Once you have a reference, you can read and change almost anything:
Try the sandbox below — each action shows the exact JavaScript that produced it:
live element
Nexus Dashboard
Senior Engineer
actions
appendChild() — add a badge
classList — the right way to manage styles
Toggling inline styles directly is fragile. The idiomatic approach: keep visual states in CSS classes and use classList to apply them:
Your CSS defines what .highlighted looks like. JavaScript just switches it on and off. This separation keeps your code clean and your styles reusable.
Creating and inserting elements
You can build new DOM nodes entirely in JavaScript and insert them anywhere:
Events — responding to user interaction
The DOM broadcasts events when things happen: clicks, keypresses, form submissions, mouse moves, page loads. You attach a listener function that runs when the event fires.
Event bubbling and capturing
When an event fires on an element, it doesn't stay there. It travels — first down the tree (capturing phase, rarely used), then back up (bubbling phase). Every ancestor that has a listener for that event type will have its handler called.
Click the button and watch the event travel upward through each ancestor:
DOM structure
event listener log
click the button…
handler code
Without stopPropagation, the event bubbles upward through every ancestor that has a listener.
Removing event listeners
Listeners attached with addEventListener stay active until explicitly removed or the element is destroyed:
Your challenge
setupCard(name, role) is pure DOM wiring. Select two elements by their selectors, set their text content, then attach a click listener to the button. The listener logs a message that uses the name parameter — which means it needs to be a closure over the function argument.
That last part is important: the listener closes over name and will still have access to it when the button is clicked later.
Challenge
Write a function called setupCard(name, role) that: (1) queries the element with id 'user-name' and sets its textContent to name, (2) queries the element with class 'user-role' and sets its textContent to role, (3) queries the button with id 'profile-btn' and adds a click event listener that logs 'Profile opened for: ' + name. Call setupCard('Jordan', 'Security Lead').