Learning Outcome: By the end of this lesson, you will know how to create and remove elements on a web page using JavaScript.
The Document Object Model (DOM) allows JavaScript to interact with HTML. Using the DOM, we can change almost everything on a web page. For example, think about a party invitation list. If someone can come, you want to add them to the list. If they can’t come, you need to remove them. In this lesson, we will focus on how to create and remove page elements effectively using JavaScript.
The DOM is a powerful interface that represents HTML documents. It allows scripts to update the content, structure, and style of a document. Understanding how to manipulate it is crucial for web developers. Therefore, being able to create and remove page elements is an essential skill.
File: index.html
<!DOCTYPE html>
<html>
<head>
<title>Create and Remove Elements</title>
</head>
<body>
<h1>Guest List</h1>
<ul id="guestList"></ul>
<input type="text" id="guestName" placeholder="Add a guest">
<button onclick="addGuest()">Add Guest</button>
<button onclick="removeGuest()">Remove Last Guest</button>
<script>
function addGuest() {
var guestName = document.getElementById("guestName").value;
var li = document.createElement("li");
li.textContent = guestName;
document.getElementById("guestList").appendChild(li);
}
function removeGuest() {
var guestList = document.getElementById("guestList");
guestList.removeChild(guestList.lastChild);
}
</script>
</body>
</html>
Expected Output:
- When you add a name, it appears in the list.
- Clicking remove will take away the last name in the list.
The important lines of code show how to alter DOM elements:
document.createElement() creates a new HTML element.element.textContent sets the text inside the new element.appendChild() adds the new element to the list.removeChild() removes the last element from the list.First, using document.createElement(), you can add any type of HTML element. Next, element.textContent allows you to specify what text appears inside it. Then, you can insert your new element into the document with appendChild(). Finally, when you need to remove an element, use removeChild().
Now, let’s practice! Change the program to let users enter multiple guests at once. Additionally, add a new button to clear the entire guest list. This exercise will reinforce your understanding of how to manage page elements.
<!DOCTYPE html>
<html>
<head>
<title>Guest List</title>
</head>
<body>
<h1>Guest List</h1>
<ul id="guestList"></ul>
<input type="text" id="guestName" placeholder="Add guests (comma separated)">
<button onclick="addGuests()">Add Guests</button>
<button onclick="clearList()">Clear List</button>
<script>
function addGuests() {
var guests = document.getElementById("guestName").value.split(", ");
for (var i = 0; i < guests.length; i++) {
var li = document.createElement("li");
li.textContent = guests[i];
document.getElementById("guestList").appendChild(li);
}
}
function clearList() {
var guestList = document.getElementById("guestList");
guestList.innerHTML = "";
}
</script>
</body>
</html>
getElementById() match the HTML elements.