Simple Todo List using JavaScript

Simple Todo List using JavaScript
Code Snippet:Pure JavaScript Todo List
Author: retsub79
Published: January 10, 2024
Last Updated: January 22, 2024
Downloads: 5,838
License: MIT
Edit Code online: View on CodePen
Read More

This JavaScript code snippet helps you to create a simple todo list. It comes with a simple interface to add task with title and its content. The added task can be deleted after done.

How to Create Simple Todo List using JavaScript

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

  <div class="todo">
    <section>
        <h1>Todo list</h1>
        <form id="form">
            <p>
                <label for="title">
                    <span>Title <strong><abbr title="required">*</abbr></strong></span>
                    <input type="text" id="title" name="title"  />
                </label>
            </p>
            <p>
                <label for="content">
                    <span>Content </span>
                    <input type="text" id="content" name="content" />
                </label>
            </p>
            <p>
                <button id="add-task" type="button">Add task</button>
            </p>
        </form>
    </section>
</div>
<div id="todos"></div>

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

@import url(https://fonts.googleapis.com/css?family=Open+Sans:400,700);
*, *:before, *:after {
  -moz-box-sizing: border-box;
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
}

body {
  background-color: #99B2B7;
  font-family: "Open Sans", sans-serif;
  font-size: 1em;
}

section {
  background-color: #D5DED9;
  padding: 30px;
  margin-bottom: 10px;
  overflow: hidden;
  max-width: 800px;
}
section:nth-child(even) {
  background-color: white;
}

h1,
h2 {
  font-weight: 200;
  font-size: 4em;
  margin: 0 0 0.5em;
  color: #99B2B7;
}

h2 {
  font-size: 2em;
}

button {
  border: 2px solid #99B2B7;
  background-color: transparent;
  padding: 10px 20px;
  cursor: pointer;
  font-size: 1.25em;
  color: #99B2B7;
  margin-top: 20px;
  text-transform: uppercase;
  text-align: center;
  float: right;
  -webkit-transition: all 0.3s;
  -moz-transition: all 0.3s;
  -ms-transition: all 0.3s;
  -o-transition: all 0.3s;
  transition: all 0.3s;
}
button:hover, button:focus {
  background-color: #99B2B7;
  color: #D5DED9;
}

label {
  font-weight: bold;
}
label span {
  margin-bottom: 5px;
  display: block;
}
label span strong {
  color: #dd6666;
}

input {
  padding: 20px;
  width: 100%;
  border: 1px solid #99B2B7;
  border-radius: 0;
  font-size: 1em;
}
input.error {
  border-color: #dd6666;
}

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

(function() {
    "use strict";

    var todoApp = (function() {
        document.addEventListener("DOMContentLoaded", function() {
            init();
        });

        var init = function() {
            document.getElementById("add-task").addEventListener("click", function() {
                var newTitle = document.getElementById("title").value,
                    newContent = document.getElementById("content").value;
                if (validate(newTitle)) {
                    addTodo(newTitle, newContent);
                }
            }, false);

            document.getElementById("todos").addEventListener("click", function(e) {
                if (e.target && e.target.nodeName == "BUTTON") {
                    deleteTodo(e.target.parentNode);
                }
            }, false);
        };

        var validate = function(newTitle, newContent) {
            if (newTitle.length === 0) {
                var target = document.getElementById("title");
                target.className = target.className + " error";
                return false;
            } else {
                return true;
            }
        };

        var addTodo = function(title, content) {
            var newTodo = document.createElement("section"),
                h2 = document.createElement("h2"),
                newTitle = document.createTextNode(title),
                p = document.createElement("p"),
                newContent = document.createTextNode(content),
                button = document.createElement("button"),
                deleteBtn = document.createTextNode("delete");

            h2.appendChild(newTitle);
            newTodo.appendChild(h2);
            p.appendChild(newContent);
            newTodo.appendChild(p);
            button.appendChild(deleteBtn);
            newTodo.appendChild(button);

            document.getElementById("todos").appendChild(newTodo);
            document.getElementById("form").reset();
        };

        var deleteTodo = function(todo) {
            todo.parentNode.removeChild(todo);
        };
    }());
}());

That’s all! hopefully, you have successfully integrated this todo 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