Beginner's Guide: JavaScript To-Do App
Beginner's Guide: JavaScript To-Do App
You have 2 free member-only stories left this month. Sign up for Medium and get an extra one
                                       In this app, we will be able to add new tasks to be done, delete tasks,
                                       mark tasks after completion, we will have a drop-down menu to filter our
                                       tasks on basis of completed or incomplete tasks.
So without any further delay let’s get started with the coding.
1. index.html
2. styles.css
3. main.js
                                             1    <html lang="en">
                                             2    <head>
                                             3           <meta charset="UTF-8">
                                             4           <meta name="viewport" content="width=device-width, initial-scale=1.0">
                                             5           <link href="https://fonts.googleapis.com/css2?family=Poppins&display=swap" rel="stylesheet">
                                             6           <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.14.0/css/
                                             7               integrity="sha512-1PKOgIY59xJ8Co8+NE6FZ+LOAZKjy+KY8iq0G4B3CyeY6wYHN3yt9PW0XpSriVlkMXe40PT
                                             8               crossorigin="anonymous" />
                                             9           <link rel="stylesheet" href="./styles.css">
                                         10              <title>To-Do List App</title>
                                         11       </head>
                                         12       <body>
                                         13              <!-- TITLE -->
                                         14              <header>
                                         15                  <h1>WORKS TO BE DONE</h1>
                                         16              </header>
                                         17              <!-- INPUT FIELD TO ENTER TASK AND DROPDOWN FILTER -->
                                         18              <form>
                                         19                  <input type="text" placeholder="Enter The Work" class="todo_input" />
                                         20                  <button class="todo_button" type="submit">
                                         21                         <i class="fas fa-plus-square"></i>
                                         22                  </button>
                                         23                  <div class="select">
                                         24                         <select name="todos" class="filter_todo">
                                         25                             <option value="all">All</option>
                                         26                             <option value="completed">Completed</option>
                                         27                             <option value="uncompleted">Uncompleted</option>
                                         28                         </select>
                                         29                  </div>
                                         30              </form>
                                         31              <!-- CONTAINER FOR DISPLAYING TO-DO LIST BY ADDING TASKS DYNAMICALLY USING JAVASCRIPT -->
                                         32              <div class="todo_container">
                                         33                  <ul class="todo_list"></ul>
                                         34              </div>
                                         35              <!-- ADDING JAVA SCRIPT -->
                                         36              <script src="./main.js"></script>
                                         37       </body>
                                         38       </html>
HTML File
                                       In body tag of our file we have three main section: 1. Heading 2. Form
                                       3.Task Container and at last we are just linking our JavaScript file.
                                       In task container section we have all our tasks which are added to our
                                       page dynamically when user adds a task through JavaScript.
CSS File
                                       This is my styling for our To-Do List app which you can easily understand
                                       by just reading once as I have also added comments specifying the role of
                                       the code. You can also come up with your own styling. And please send
                                       the link of your styling in comment section. I would love to see all of your
                                       creative styling.
                                             1    //selectors
                                             2    const todoInput = document.querySelector('.todo_input');
                                             3    const todoButton = document.querySelector('.todo_button');
                                             4    const todoList = document.querySelector('.todo_list');
                                             5    const filterOption = document.querySelector('.filter_todo');
                                             6    //event listeners
                                             7    todoButton.addEventListener("click", addTodo)
                                             8    todoList.addEventListener("click", deleteCheck)
                                             9    filterOption.addEventListener("click", filterTodo)
                                         10       //functions
                                         11
                                         12       function addTodo(event) {
                                         13              event.preventDefault();
                                         14              //todo DIV
                                         15              const todoDiv = document.createElement('div');
                                         16              todoDiv.classList.add('todo');
                                         17              //todo LI
                                         18              const newTodo = document.createElement('li');
                                         19              newTodo.innerText = todoInput.value;
                                         20              newTodo.classList.add('todo_item');
                                         21              todoDiv.appendChild(newTodo);
                                         22              if(todoInput.value === ""){
                                         23                  return null
                                         24              }
                                         25              //check mark BUTTON
                                         26              const completedButton = document.createElement('button');
                                         27              completedButton.innerHTML = '<i class="fas fa-check"></i>';
                                         28              completedButton.classList.add('complete_btn')
                                         29              todoDiv.appendChild(completedButton);
                                         30              //delete BUTTON
                                         31              const deleteButton = document.createElement('button');
                                         32              deleteButton.innerHTML = '<i class="fas fa-trash"></i>';
                                         33              deleteButton.classList.add('delete_btn')
                                         34              todoDiv.appendChild(deleteButton);
                                         35              //Append to Actual LIST
                                         36              todoList.appendChild(todoDiv);
                                         37              //Clear todo input VALUE
                                         38              todoInput.value = ""
                                         39       }
                                         40
                                         41       //DELETE & CHECK
                                         42       function deleteCheck(e) {
                                         43              const item = e.target;
                                         44              //DELETE ITEM
                                         45              if (item.classList[0] === "delete_btn") {
                                         46                  const todo = item.parentElement;
                                         47                  //ANIMATION TRANSITION
                                         48                  todo.classList.add("fall")
                                         49                  todo.addEventListener('transitionend', function () {
                                         50                         todo.remove()
                                         51                  })
                                         52              }
                                         53              //COMPLETE ITEM
                                         54              if (item.classList[0] === "complete_btn") {
                                         55                  const todo = item.parentElement;
                                         56                  todo.classList.toggle("completedItem")
                                         57              }
                                         58       }
                                         59       //FILTERING THE TASKS ACCORDING THE OPTION
                                         60       function filterTodo(e) {
                                         61              const todos = todoList.childNodes;
                                         62              for(let i = 1; i<todos.length; i++ ){
                                         63                  switch (e.target.value) {
                                         64                         case "all":
                                         65                             todos[i].style.display = "flex";
                                         66                             break;
                                         67                         case "completed":
                                         68                             if (todos[i].classList.contains('completedItem')) {
                                         69                                 todos[i].style.display = "flex";
                                         70                             } else {
                                         71                                 todos[i].style.display = "none";
                                         72                             }
                                         73                             break;
                                         74                         case "uncompleted":
                                         75                             if (!todos[i].classList.contains('completedItem')) {
                                         76                                 todos[i].style.display = "flex";
                                         77                             } else {
                                         78                                 todos[i].style.display = "none";
                                         79                             }
                                         80                             break;
                                         81                  }
                                         82              }
                                         83       }
                                         84
JavaScript File
                                       Here comes the exciting part for which you guys are reading this. The
                                       JavaScript file is responsible for all the functionality of our app.
//selectors
//event listeners
todoButton.addEventListener("click", addTodo)
todoList.addEventListener("click", deleteCheck)
filterOption.addEventListener("click", filterTodo)
function addTodo(event) {
event.preventDefault();
//todo DIV
todoDiv.classList.add('todo');
//todo LI
newTodo.innerText = todoInput.value;
newTodo.classList.add('todo_item');
todoDiv.appendChild(newTodo);
return null;
completedButton.classList.add('complete_btn')
todoDiv.appendChild(completedButton);
//delete BUTTON
deleteButton.classList.add('delete_btn')
todoDiv.appendChild(deleteButton);
todoList.appendChild(todoDiv);
todoInput.value = ""
                                       This addTodo function will execute when the add button on input will be
                                       clicked. This function is responsible for adding a task, adding check
                                       button and adding delete button.
function deleteCheck(e) {
//DELETE ITEM
//ANIMATION TRANSITION
todo.classList.add("fall")
todo.addEventListener('transitionend', function () {
todo.remove()
})
//COMPLETE ITEM
todo.classList.toggle("completedItem")
                                       In this function we are getting the target element using e.target. Then
                                       we are checking if the target element is delete button or check button. If
                                       it is delete button(delete_btn) then we are simply getting its parent
                                       element with .parentElement property and deleting it with the help of
                                       .remove() method after the transition is completed which is added by
                                       adding ‘fall’ class to the whole <div>. If we clcik on check button
                                       (complete_btn) then we are just toggling a class to the parent element
                                       that is <div> itself which will apply some styling to the task to confirm
                                       that this task is completed.
function filterTodo(e) {
switch (e.target.value) {
case "all":
todos[i].style.display = "flex";
break;
case "completed":
if (todos[i].classList.contains('completedItem')) {
todos[i].style.display = "flex";
} else {
todos[i].style.display = "none";
break;
case "uncompleted":
if (!todos[i].classList.contains('completedItem')) {
todos[i].style.display = "flex";
} else {
todos[i].style.display = "none";
break;
todos[i].style.display = "flex";
} else {
todos[i].style.display = "none";
                                       That is all for today. I hope this was helpful for guys. I will be posting my
                                       ideas on various topics and we will build many cool apps together.
257 1
Next.js Project Structure                          Koop.js in Layman’s                                    Business Data Extractor                      PEG away at evaluating
Yannick Wittwer
                                                   Terms                                                  with Google Places API,                      expressions in JavaScript
                                                   Lusi Suwandi
                                                                                                          Next.Js, and Node                            Federico Kereki in Globant
                                                                                                          Diligent Dev
The easiest way I know to                          Think Functionally and                                 A comprehensive guide                        How to unit test your first
replace icons in a React                           Become Functional in                                   to using BEM with React                      Vue.js component
app                                                JavaScript                                             Asís García in Trabe                         Sarah Dayan in
Tom Parandyk in Views Tools                        Kretawiweka Nuraga Sani in                                                                          freeCodeCamp.org
                                                   The Startup