The DOM and Events: Select Elements, Change Content and Classes, and Build a Menu Toggle and Accordion
What Is the DOM?
The DOM (Document Object Model) is the page as a tree of objects that JavaScript can read and change. Every tag becomes a node.
Select and Change
javascript
const msg = document.querySelector("#msg"); // first match
const links = document.querySelectorAll("nav a"); // all matches
msg.textContent = "Welcome back!"; // safe: plain text
msg.classList.add("highlight"); // also: remove, toggle, contains
msg.setAttribute("data-state", "ready");
Safety Avoid putting user input into innerHTML. It can run harmful scripts (XSS). Use textContent for text.
Events
javascript
const button = document.querySelector("#save");
button.addEventListener("click", (event) => {
console.log("clicked", event.target);
});
Common events: click, input, change, submit, keydown, focus, scroll, DOMContentLoaded.
Project 1: Accessible Menu Toggle
html
<header>
<button id="menu-btn" aria-expanded="false" aria-controls="menu">Menu</button>
<nav id="menu" hidden>
<a href="/">Home</a>
<a href="/cakes.html">Cakes</a>
</nav>
</header>
javascript
const btn = document.querySelector("#menu-btn");
const menu = document.querySelector("#menu");
btn.addEventListener("click", () => {
const open = btn.getAttribute("aria-expanded") === "true";
btn.setAttribute("aria-expanded", String(!open));
menu.hidden = open;
});
aria-expanded tells screen readers whether the menu is open
aria-controls links the button to the menu
- The
hidden attribute hides content from everyone, including assistive tech
- A real
button works with keyboard Enter and Space for free
Project 2: Accordion
html
<div class="accordion">
<button class="acc-btn" aria-expanded="false" aria-controls="p1">Do you deliver?</button>
<div id="p1" hidden>We deliver within 2 hours in Pune.</div>
</div>
javascript
document.querySelectorAll(".acc-btn").forEach((b) => {
b.addEventListener("click", () => {
const panel = document.getElementById(b.getAttribute("aria-controls"));
const open = b.getAttribute("aria-expanded") === "true";
b.setAttribute("aria-expanded", String(!open));
panel.hidden = open;
});
});
Tested Behavior
We ran this code against a DOM test environment and clicked the buttons from a script. Results:
Result
message text: Welcome back! | menu hidden before: true | after first click: false (aria-expanded true) | after second click: true | accordion panel hidden after click: false
Event Delegation
Instead of adding a listener to many items, add one listener to the parent and check what was clicked. It works for items added later too.
javascript
document.querySelector("#list").addEventListener("click", (e) => {
const item = e.target.closest("li");
if (item) item.classList.toggle("done");
});
Practice
- Build the menu toggle and close it when Escape is pressed
- Make the accordion open only one panel at a time
- Toggle a
done class on list items with event delegation
Conclusion
Select elements, change them and listen for events. Use buttons, aria-expanded and hidden for interactive parts that everybody can use.
Quick Summary: DOM; select; events; menu; accordion; test; delegation.