JavaScript DOM Manipulation
Day 4: Build an Interactive Calculator – Make Your Webpage Come Alive
Welcome to Day 4
In Days 1–3, you learned variables, functions, and control flow — JavaScript "thinking" behind the scenes. Today you connect that thinking to the actual webpage the user sees. This is called DOM Manipulation, and it's what makes websites truly interactive.
By the end of today, you'll have built a fully working calculator in pure JavaScript — no page reloads, no frameworks, just HTML, CSS, and JS working together.
Today's Goal: Understand how JavaScript reads from and writes to a webpage. Then apply all of it in one real project — an interactive calculator with add, subtract, multiply, and divide operations.
What You'll Build Today
Step 1 – Learn the DOM
Understand the "Document Object Model" — how JavaScript sees your HTML page
Step 2 – Select Elements
Usedocument.querySelector()to grab buttons, inputs, and divs
Step 3 – Listen for Events
Detect button clicks and user interactions withaddEventListener()
Step 4 – Read & Update the Page
Get input values and display results without refreshing
Step 5 – Build the Calculator
Put it all together in one real, working project
What is the DOM?
When a browser loads your HTML page, it doesn't just display it — it also builds a tree-like map of every element on the page. This map is called the DOM (Document Object Model).
Think of it this way: your HTML file is like a blueprint. The DOM is the actual building that gets constructed from that blueprint. JavaScript can then walk through that building, open doors (read content), paint walls (change styles), or even knock walls down (remove elements).
Your HTML file:
<html> <body> <h1 id="title">Hello</h1> <p class="description">Welcome!</p> <button id="myBtn">Click Me</button> </body> </html>How the DOM sees it (as a tree):
📄 document│🌐│📦┌──────────────┼──────────────┐🔠
id="title"📝
class="description"🔘JavaScript can grab any of these nodes by their tag, id, or class — then read or change them.
Key Idea: The DOM is JavaScript's view of your HTML. Every HTML element becomes a JavaScript object that you can access and modify. This is what makes web pages dynamic.
The document Object
In JavaScript, document is your entry point into the DOM. It represents the entire webpage and has built-in methods to find elements.
// The document object represents the whole page console.log(document); // the entire page console.log(document.title); // the page title console.log(document.body); // the <body> element
document.querySelector() — Selecting Elements
document.querySelector() is how you tell "Go find this specific element on the page and give it to me."
It uses the same selectors as CSS — so if you know how to target elements with CSS, you already know how to use querySelector.
Selecting by ID (most common — use #)
// HTML: <h1 id="title">Hello World</h1> let titleElement = document.querySelector('#title'); // Now titleElement IS the <h1> element // You can read its content: console.log(titleElement.textContent); // "Hello World"Selecting by Class (use .)
// HTML: <p class="description">Welcome!</p> let desc = document.querySelector('.description'); console.log(desc.textContent); // "Welcome!"Selecting by Tag Name
// Selects the FIRST <button> it finds let btn = document.querySelector('button'); console.log(btn.textContent); // "Click Me"
Visual: querySelector in Action
HTML (what the user sees):
id="num1" type="number">
id="result">Result appears hereJavaScript (selecting those elements):
let num1Input = document.querySelector('#num1');
let addButton = document.querySelector('#addBtn');
let resultDiv = document.querySelector('#result');num1Input
Points to the input box — you can read what the user typedaddButton
Points to the button — you can listen for clicksresultDiv
Points to the result area — you can write output here
querySelectorAll — Selecting Multiple Elements
When you need all matching elements (not just the first), use querySelectorAll(). It returns a list.
// Select ALL buttons on the page let allButtons = document.querySelectorAll('button'); // Loop through them allButtons.forEach(function(btn) { console.log(btn.textContent); }); // Prints: "Add", "Subtract", "Multiply", "Divide"
Quick Reference:
'#myId'→ selects element withid="myId"
'.myClass'→ selects element withclass="myClass"
'button'→ selects a<button>element
Event Listeners — Responding to User Actions
An event is anything that happens on a webpage — a click, a keypress, a mouse hover, a form submission. An event listener is code that waits and watches for a specific event, then runs a function when it happens.
The Syntax:
element.addEventListener('eventType', function() { // code to run when the event happens });element
The HTML element to watch (e.g., a button)'click'
The event to listen forfunction() { }
Code to run when it happens
Click Events
Simple button click:
// HTML: <button id="greetBtn">Say Hello</button> let greetBtn = document.querySelector('#greetBtn'); greetBtn.addEventListener('click', function() { console.log("Button was clicked!"); alert("Hello, World!"); });What happens step by step:
Step 1Page loads → event listener is set up and waits silently
Step 2