Build Web Applications • Reading lesson • Free
javascript and the dom No account is required. Learn the material, try the examples, and mark it complete locally when you are ready to move on.
Tutorial 13 - JavaScript and the DOM Why this tutorial exists The DOM can sound technical.
A beginner-friendly definition is:
The DOM is the browser's representation of the HTML page. JavaScript can use the DOM to find and change elements.
1. Start with HTML html
<p id="message">Hello</p>JavaScript can find it:
javascript
const message =
document.getElementById("message");Now message refers to the paragraph element.
2. Change the page javascript
message.textContent =
"Hello from JavaScript";The visible text changes.
The flow:
Need a human teacher?
This lesson stays free. Join a CookieSensei cohort only if you want live classes, feedback, cloud labs and personal guidance.
Explore guided cohorts Open learning promise
No sign-up wall. No email gate. Share this lesson with anyone who may find it useful.
HTML
↓
Browser creates DOM
↓
JavaScript finds element
↓
JavaScript changes element
↓
Browser updates page
3. Button example html
<button id="hello-button">
Say Hello
</button>
<p id="message"></p>javascript
const button =
document.getElementById("hello-button");
const message =
document.getElementById("message");
button.addEventListener(
"click",
function () {
message.textContent = "Hello!";
}
);text
click
↓
event
↓
function
↓
DOM change
4. Why IDs matter javascript
getElementById("message")
5. Other selectors javascript
document.querySelector(".button");javascript
document.querySelector("#message");These are ways to find elements.
You do not need to memorize every selector.
6. Changing classes JavaScript can modify CSS classes:
javascript
message.classList.add("success");javascript
message.classList.remove("hidden");This is commonly used to show, hide, or style elements.
7. Reading input html
<input id="name" type="text">
<button id="show">Show</button>
<p id="result"></p>javascript
const nameInput =
document.getElementById("name");
const result =
document.getElementById("result");
const button =
document.getElementById("show");
button.addEventListener(
"click",
function () {
result.textContent =
nameInput.value;
}
);The browser reads the input and updates the page.
8. DOM vs Django Django primarily runs on the server.
The DOM exists in the browser.
text
Server
↓
Django
↓
HTML
↓
Browser
↓
DOM
↓
JavaScripttext
Django → server-side application
JavaScript → browser-side behavior
9. Debugging If a button does nothing:
Check HTML
Check JavaScript javascript
getElementById("hello-button")
Check Console text
Developer Tools → Console
Check Network Did the JavaScript file actually load?
10. Exercise text
Name: [________]
[Say Hello]
Hello, ______!
11. Why this is enough React Next.js browser APIs JavaScript libraries third-party widgets This is browser-side code selecting elements, responding to events, and changing the page. That is the intended Phase 4 outcome.
12. Using ChatGPT Here is my HTML and JavaScript. Explain which DOM elements are selected, which events are handled, and what changes when the user clicks. Do not rewrite the code.
Remember text
HTML
↓
DOM
↓
JavaScript
↓
event
↓
function
↓
DOM changes
↓
browser updatesFinished this lesson? Completion is saved in this browser without creating an account.
Mark lesson complete