- Published on
Part 6: Manipulating the DOM
- Authors
- Name
- Diego Herrera Redondo
- @diegxherrera
Welcome back to our JavaScript Crash Course series! π So far, we've explored many programming fundamentals, like functions, arrays, objects, and methods to work with them. Now, in Part 6, weβre going to dive into DOM manipulation. DOM (Document Object Model) manipulation is what makes web pages dynamic and interactive. Ready to start making your web pages come to life? Letβs go! π
What is the DOM? π
The Document Object Model (DOM) is a representation of the HTML structure of a web page. When a browser loads an HTML document, it creates a model of that document, which JavaScript can access and modify. Using JavaScript, you can dynamically change the content, structure, and style of the webpage.
Think of the DOM as a tree structure where each HTML element is a node. By manipulating these nodes, we can change the way the page looks and behaves.
Selecting Elements in the DOM π―
To manipulate the DOM, we first need to select the elements we want to change. JavaScript provides several methods for this:
getElementById()
1. Selects an element by its id
attribute.
<div id="myElement">Hello!</div>
let element = document.getElementById("myElement");
console.log(element); // Output: <div id="myElement">Hello!</div>
getElementsByClassName()
2. Selects all elements with a specific class. It returns a HTMLCollection, which is similar to an array.
<div class="item">Item 1</div>
<div class="item">Item 2</div>
let items = document.getElementsByClassName("item");
console.log(items[0]); // Output: <div class="item">Item 1</div>
querySelector()
and querySelectorAll()
3. querySelector()
selects the first element that matches a CSS selector.querySelectorAll()
selects all elements that match a CSS selector.
<div class="item">Item 1</div>
<div class="item">Item 2</div>
let firstItem = document.querySelector(".item");
console.log(firstItem); // Output: <div class="item">Item 1</div>
let allItems = document.querySelectorAll(".item");
console.log(allItems); // Output: NodeList(2) [div.item, div.item]
Modifying Elements π¨
Once we've selected an element, we can modify its content, attributes, and styles.
Changing Content
You can update the inner text or HTML of an element.
let element = document.getElementById("myElement");
element.innerText = "New Text!"; // Updates only the text content
element.innerHTML = "<strong>Bold Text!</strong>"; // Adds HTML inside
Changing Attributes
Use setAttribute()
and getAttribute()
to change or retrieve attributes.
<img id="myImage" src="old-image.jpg" />
let image = document.getElementById("myImage");
image.setAttribute("src", "new-image.jpg"); // Changes the src attribute
Changing Styles
You can modify the style
property of an element to apply CSS directly.
let element = document.getElementById("myElement");
element.style.color = "blue";
element.style.fontSize = "20px";
Creating and Removing Elements π οΈ
JavaScript allows you to add new elements to the DOM or remove existing ones.
Creating an Element
- Use
document.createElement()
to create a new element. - Set its content or attributes as needed.
- Add it to the DOM using
appendChild()
orinsertBefore()
.
let newElement = document.createElement("p");
newElement.innerText = "I'm a new paragraph!";
document.body.appendChild(newElement); // Adds to the end of the body
Removing an Element
To remove an element, use the removeChild()
method on its parent.
<div id="container">
<p id="paragraph">Remove me!</p>
</div>
let container = document.getElementById("container");
let paragraph = document.getElementById("paragraph");
container.removeChild(paragraph); // Removes the paragraph
Event Listeners π
Event listeners allow JavaScript to listen for user interactions, like clicks, typing, or mouse movements, and respond to them. This is how we make our pages interactive.
Adding an Event Listener
To add an event listener, use the addEventListener()
method.
<button id="myButton">Click me!</button>
let button = document.getElementById("myButton");
button.addEventListener("click", () => {
alert("Button was clicked!");
});
In this example, clicking the button will trigger an alert.
Common Events
Some popular events include:
click
: When an element is clicked.mouseover
: When the mouse hovers over an element.mouseout
: When the mouse leaves an element.input
: When the user types in an input field.submit
: When a form is submitted.
Practice Challenge: Interactive To-Do List π
Let's build a simple interactive to-do list using DOM manipulation and events!
- Create an HTML structure with an input field, a button, and an empty list.
- Write JavaScript to add new tasks to the list when the button is clicked.
- Add a "Remove" button next to each task to delete it when clicked.
Example Solution
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>To-Do List</title>
</head>
<body>
<input type="text" id="taskInput" placeholder="Add a new task" />
<button id="addTaskBtn">Add Task</button>
<ul id="taskList"></ul>
<script>
const addTaskBtn = document.getElementById("addTaskBtn");
const taskInput = document.getElementById("taskInput");
const taskList = document.getElementById("taskList");
addTaskBtn.addEventListener("click", () => {
// Get the input value
const taskText = taskInput.value;
if (taskText === "") return;
// Create a new list item
const listItem = document.createElement("li");
listItem.innerText = taskText;
// Add a "Remove" button
const removeBtn = document.createElement("button");
removeBtn.innerText = "Remove";
removeBtn.addEventListener("click", () => {
taskList.removeChild(listItem);
});
// Add the remove button to the list item
listItem.appendChild(removeBtn);
// Add the list item to the task list
taskList.appendChild(listItem);
// Clear the input
taskInput.value = "";
});
</script>
</body>
</html>
Wrapping Up
In this part, we explored DOM manipulation and learned how to dynamically update web pages with JavaScript. By selecting elements, modifying them, and responding to events, you can create interactive experiences on your web pages.
In Part 7, weβll dive into JavaScript events in more depth and explore how to handle them effectively in complex applications. Keep practicing, and let your creativity shine! ποΈ
Thanks for following along, and happy coding! π