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.
Similar Code Snippets:
I code and create web elements for amazing people around the world. I like work with new people. New people new Experiences.
I truly enjoy what I’m doing, which makes me more passionate about web development and coding. I am always ready to do challenging tasks whether it is about creating a custom CMS from scratch or customizing an existing system.