The page as a tree
When the browser reads HTML it builds a tree of objects: the document contains html, which contains head and body, and so on. Each element is a node you can find and change. Changes appear on screen immediately, but they do not change your HTML file.
Finding elements
const title = document.querySelector("h1"); // first match
const cards = document.querySelectorAll(".card"); // all matches
const form = document.querySelector("#signup"); // by id
console.log(cards.length);
cards.forEach(card => console.log(card.textContent));
Both methods take a CSS selector, the same kind you write in a stylesheet. querySelector returns null if nothing matches, which is the usual cause of the error Cannot read properties of null.
Changing text, classes and styles
title.textContent = "Welcome back!";
title.classList.add("highlight");
title.classList.toggle("hidden");
title.style.color = "#17594f";
document.querySelector("img").alt = "Students in a science lab";
- Use
textContentto set text. AvoidinnerHTMLwith anything a user typed: it would run as HTML and can let attackers inject code. - Prefer adding and removing classes over setting styles one by one, and keep the look in CSS.
Creating and removing elements
const list = document.querySelector("#names");
function addName(name) {
const item = document.createElement("li");
item.textContent = name;
list.append(item);
}
addName("Arta");
addName("Dren");
list.firstElementChild.remove();
createElement makes a new element, append adds it at the end of a parent, and remove takes an element off the page.
Complete example: a name list
<form id="add-form">
<label for="name">Name</label>
<input id="name" required>
<button>Add</button>
</form>
<ul id="names"></ul>
<p id="total">0 names</p>
<script>
const form = document.querySelector("#add-form");
const input = document.querySelector("#name");
const list = document.querySelector("#names");
const total = document.querySelector("#total");
function updateTotal() {
total.textContent = list.children.length + " names";
}
form.addEventListener("submit", event => {
event.preventDefault(); // stop the page reloading
const item = document.createElement("li");
item.textContent = input.value.trim() + " ";
const remove = document.createElement("button");
remove.textContent = "Remove";
remove.className = "remove";
item.append(remove);
list.append(item);
input.value = "";
input.focus();
updateTotal();
});
// One listener on the list handles every Remove button, even new ones.
list.addEventListener("click", event => {
if (event.target.matches(".remove")) {
event.target.closest("li").remove();
updateTotal();
}
});
</script>
The second listener uses event delegation: clicks bubble up from the button to the list, so one listener on the parent works for buttons created later.
Try it yourself
Press "Try it" on the complete example to open it in Unays Code with HTML preview. Add a "Clear all" button and stop the same name being added twice. Then compare with the bigger to-do list project.
This tutorial was written with AI assistance and reviewed by Unays. The runnable code examples were tested before publishing. Spotted a mistake? Tell us at [email protected].