Week 4: DOM Manipulation
To Do This Week:
Assignments:
Read (Optional!): Chapters from Eloquent JavaScript:
Upcoming Assignments
- Clickable Page (5%) — no AI-generated code (Due Week 6 — 9/30)
Module Notes
This week we move from working with data (arrays, iterators) to working with the page itself. The DOM (Document Object Model) is the browser's live, tree-shaped representation of your HTML. JavaScript can reach into that tree to select elements, read and change them, add or remove them, and respond to what the user does.
The whole loop for the Clickable Page is: select an element → listen for a click → change something on the page. Everything below is a variation on that loop.
The DOM as a Tree
Your HTML nests elements inside elements. The browser turns that nesting into a tree of nodes. document is the root. From there you can travel down to children, up to parents, and sideways to siblings.
In-class Exercise:
DOM Playground: Buttons, Boxes & Menus- select an element
- click > function
- change text / change CSS
- toggle a class
- show / hide
- add / remove elements
- slide a menu open and closed
1. Selecting Elements
-
document.getElementById("box")
Selects one element by its ID. -
document.querySelector(".card")
Selects the first element that matches a CSS selector. -
document.querySelectorAll(".card")
Selects all matching elements (returns a NodeList you can loop over). -
document.getElementsByClassName("card")
Selects all elements with a class name (returns a live HTMLCollection).
Tip: Store your selection in a variable so you don't re-select every time.
const box = document.getElementById("box");
2. Reading & Changing Content
-
element.textContent = "New text"
Changes the plain text inside an element. -
element.innerHTML = "<em>New</em> text"
Changes the HTML inside an element (use carefully). -
element.value
Reads or sets the value of an input, textarea, or select. -
element.setAttribute("src", "cat.png")
Changes an attribute (like an image source or a link).
3. Changing CSS with JavaScript
document.body.style.backgroundColor = "lightblue"
// change the background color of the whole pagedocument.getElementById("box").style.width = "300px"
// set the width of an element with id="box"document.querySelector("h1").style.color = "red"
// change the text color of the first <h1>box.style.border = "2px solid black"
// add a border around the elementdocument.querySelector(".demo").style.fontSize = "24px"
// change the font size of the first element with class="demo"
4. Toggling Classes (the clean way)
Instead of setting many styles one by one in JavaScript, define a class in CSS and switch it on or off. This is how most professional interactive pages work.
-
element.classList.add("active")
Adds a class. -
element.classList.remove("active")
Removes a class. -
element.classList.toggle("active")
Adds the class if it's missing, removes it if it's there. Perfect for on/off buttons. -
element.classList.contains("active")
Returns true or false — useful in a conditional.
/* CSS */
.box { transition: all 0.3s ease; }
.box.active { background: gold; transform: scale(1.2); }
// JS
const box = document.getElementById("box");
box.addEventListener("click", function () {
box.classList.toggle("active");
});
5. Listening for Clicks
-
element.addEventListener("click", function () { ... })
Runs the function every time the element is clicked.
// Demo
const btn = document.getElementById("myButton");
btn.addEventListener("click", function () {
document.body.style.backgroundColor = "lightyellow";
});
6. Show / Hide Elements
-
element.style.display = "none"// hide it
element.style.display = "block"// show it -
element.hidden = true// another way to hide
element.hidden = false// show
7. Creating & Removing Elements
-
document.createElement("p")
Creates a new element (not on the page yet). -
parent.appendChild(element)
Adds the new element inside a parent. -
element.remove()
Removes an element from the page.
// Demo
const output = document.getElementById("output");
const p = document.createElement("p");
p.textContent = "Hello world";
output.appendChild(p);
Debugging with the Console
Open your browser's Developer Tools (right-click → Inspect → Console, or Cmd/Ctrl + Shift + J). The Console is where you check whether your code is doing what you think it is.
console.log(box)// did I actually select the element? (null means no)console.log("clicked!")// did my click function even run?console.log(typeof value, value)// what type of thing am I working with?
Common bugs to watch for:
- Selecting an element before it exists on the page — put your
<script>at the bottom of the body, or useDOMContentLoaded. - Spelling the id/class differently in HTML and JS.
- Forgetting the
.for a class or#for an id insidequerySelector. Cannot read properties of nullalmost always means your selection returned nothing.
Project Introduction: Clickable Page (5%) DUE: Week 6 — 9/30
Build a single web page that responds to user interaction. Clicking elements should trigger visible changes such as content updates, CSS transitions, color or style changes, animated movement, or the appearance and disappearance of elements.
Plan the logic in plain English before writing code. Your grade will emphasize clear logic, working JavaScript, purposeful interaction, and a coherent visual design.
Minimum Requirements
- At least three clickable elements
- At least three visible changes produced with JavaScript
- DOM selection and manipulation
- At least one CSS transition or animated change
- Numbered logic steps in a comment before the script
- No AI-generated code
Logic Steps (required)
At the very top of your <script> tag, before any code, write your numbered logic steps as a commented list. Example:
/*
1. Select the button and the box.
2. When the button is clicked, run a function.
3. Inside the function, toggle the "active" class on the box.
4. The .active class in CSS changes color and size with a transition.
5. Also change the button's text to say "On" or "Off".
*/
This comment block is part of your grade. Without it, the project is considered incomplete.
Workshop (Thursday)
Bring your idea and a first draft of your logic steps. We'll work them out together and start coding. During the workshop you may:
- Use Google and documentation (MDN, W3Schools) to look up specific syntax.
- Use AI to explain a specific step or concept — not to generate your project code.
- Use the Console constantly to check selections and debug.
Some starting ideas
- Mood switcher: buttons change the page's colors, fonts, and a headline.
- Reveal cards: click a card to flip it or reveal hidden text.
- Toggle gallery: buttons show/hide groups of images.
- Slide-out menu: a button slides a nav panel in and out.
- Weird art toy: clicks move, grow, recolor, or multiply elements. The stranger the better.