To Do List Project in JavaScript

To Do List Project in JavaScript
Code Snippet:Todo App A Pen created on Code
Author: Keinan Pace
Published: January 11, 2024
Last Updated: January 22, 2024
Downloads: 1,888
License: MIT
Edit Code online: View on CodePen
Read More

This JavaScript project helps you to create a to do list to add/remove tasks. It comes with a decent interface to add tasks and mark them as done. Users can also clear the task list with one click.

How to Create To Do List Project in JavaScript

1. First of all, create the HTML structure as follows:

  <div class="phone">
  <div class="header">
    <h2>Pure JS Todo</h2>
    <p>Revisiting the beauty of pure javascript</p>
  
    <div class="removeText">🗑 Clean Checked Tasks</div>
  </div>
  <div class="todo-body">
    <ul class="todo-list">
      <li class="todo-item"><input type="checkbox"/> Welcome to your todo list</li>
    </ul>
  </div>
  <input type="text" class="todoInput" placeholder="Enter a task and press enter"/>
</div>
<!-- partial -->
  <script  src="./js/script.js"></script>

2. After that, add the following CSS styles to your project:

*{
  margin:0;
  padding:0 ;
  box-sizing: border-box !important;
}
 body {
    font-family: -apple-system, BlinkMacSystemFont, sans-serif !important;
   background: #f4f4f4;
}

.phone{
  position:relative;
  margin:4% auto;
  width:360px;
  height:660px;
  border:1px solid #e6e6e6;
  border-radius:8px;
  overflow:hidden;
  background:#fff;
}

.header{
  position:relative;
  padding:25px;
  width:100%;
  background: linear-gradient(to right, #ff416c, #ff4b2b); 
  color:#ffffff;
  line-height:28px !important;
}
.removeText{
  font-weight:bolder;
  cursor:pointer;
  padding:4px 15px;
  display:inline;
  border-radius:30px;
  text-align:center;
  border:1px solid #fff;
}

.header p{
  font-size:14px; 
}

.todo-body{
  position:relative;  
  width:100%;
  height:500px;
  overflow-y:scroll;
}

.todo-body::-webkit-scrollbar{
    background:transparent;
  width:1px;
}
.todo-list{
    list-style:none;
}

.todo-list .todo-item{
    padding:20px;
    border-bottom:1px solid #e7e7e7;
    color:#717171;
}

.todo-item.checked{
  text-decoration: line-through;
}

input[type="checkbox"]{
    position:relative;
    top:3px;
    margin-right:10px;
    width:17px;
    height:17px;

}

.todoInput{
  position:absolute;
  bottom:0;
  left:0;
  padding:20px;
  width:100%;
  border:0;
  border-top: 1px solid #e7e7e7;
}

3. Finally, add the following JavaScript code and done.

class TodoList {
    constructor() {
        this.todos = new Map();
        this.todoContainer = document.querySelector(".todo-body");
        this.todoList = document.querySelector(".todo-list");
        this.todoInput = document.querySelector(".todoInput");
        this.removeButton = document.querySelector(".removeText");
        this.bindEvents();
    }

    bindEvents() {
        this.todoInput.onkeyup = (e) => {
            if (e.keyCode === 13) {
                this.addTodo(e.target.value);
                this.todoInput.value = "";
            }
        }

        this.todoList.onmouseup = (e) => {
            if (e.target.checked !== undefined) {
                let id = e.target.getAttribute("data-key");
                this.markTodo(id, e.target.checked);
            }
        }
        this.removeButton.onclick = this.clean.bind(this);
    }
    markTodo(id, isChecked) {
        let obj = this.todos.get(id);
        obj.checked = !isChecked;
        this.todos.set(id, obj);
        this.render();
    }
    addTodo(text = "Blank Task") {
        let id = Date.now() + "";
        this.todos.set(id, {
            id: id,
            text: text,
            checked: false
        });
        this.render();
    }
    clean() {
        this.todos.forEach((todo, key) => {
            if (todo.checked) {
                this.todos.delete(key)
            }
        });
        this.render();
    }
    template(item, id) {
        return (`<li class="todo-item ${(item.checked ? "checked" : "")}" data-key="${id}"><input type="checkbox" data-key="${id}" ${(item.checked ? "checked" : "")}/> ${ item.text }</li>`);
    }
    render() {
        let todoElements = [];
        this.todos.forEach((item, key) => {
            todoElements.push(this.template(item, key))
        });

        this.todoList.innerHTML = todoElements.join(" ")
    }

}

if (document.readyState === "complete" || document.addEventListener) {
    const List = new TodoList();
}

That’s all! hopefully, you have successfully integrated this to do list code snippet into your project. If you have any questions or facing any issues, feel free to comment below.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

About CodeHim

Free Web Design Code & Scripts - CodeHim is one of the BEST developer websites that provide web designers and developers with a simple way to preview and download a variety of free code & scripts. All codes published on CodeHim are open source, distributed under OSD-compliant license which grants all the rights to use, study, change and share the software in modified and unmodified form. Before publishing, we test and review each code snippet to avoid errors, but we cannot warrant the full correctness of all content. All trademarks, trade names, logos, and icons are the property of their respective owners... find out more...

Please Rel0ad/PressF5 this page if you can't click the download/preview link

X