Adding Checkboxes to Mark Completion

Lesson Overview

Welcome to another interactive session in our course, "Making a Dynamic Todo List with JavaScript." Today, we are stepping into one of the most critical parts of any todo list — checking off items. This lesson will guide you through how to add checkboxes, make them interactive, and solve some problems that arise with this change.

Our journey will include topics like checkboxes, handling change events, manipulating CSS properties like text decorations in JavaScript, and implementing a delete button. With these functionalities, our todo list will be that much closer to being a fully interactive application.

These skills are essential not just for todo lists but also for setting preferences in forms or managing project tasks in a collaborative tool. Ready for the journey? Let's roll!

A Closer Look at Checkboxes

Let's begin our journey by examining the concept of checkboxes in HTML and JavaScript. In web development, checkboxes are commonly used for selection purposes. As you may have seen in online forms or surveys, checkboxes permit multiple selections from a group of options.

Just like text fields, checkboxes are a way to receive input from the user. In our todo list, we use checkboxes to signify whether a task is completed. Let's consider the following HTML snippet to create a new checkbox:

HTML
<input type="checkbox">

This is useful when you are adding static items to the webpage, but in our case, where we want to dynamically add new checkboxes to any tasks that may be added to the todo list, it makes more sense to use JavaScript instead:

JavaScript
const checkbox = document.createElement('input');
checkbox.type = 'checkbox';
li.appendChild(checkbox);

Here, we are using document.createElement to create an input element of type checkbox, and then appending it as a child to our list item (li). Checkboxes add interactivity to our list, allowing us to check off tasks once they're completed. Think of it like checking off items on a grocery list or marking attendance in a class.

The variable is defined using the const keyword, indicating that its reference cannot be reassigned after initial declaration. This reduces the risk of bugs caused by accidental reassignment, making the code more predictable and easier to debug. In our todo list, using const ensures the reference to elements like checkboxes remains constant while still allowing their properties to change.

Sign up

Join the 1M+ learners on CodeSignal

Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal