Make a Todo list using JavaScript


in this tutorial, we will see how to make a todo list using HTML, CSS, and JavaScript, it's a very simple and quick tutorial and by the end of the article you will be able to manipulate the DOM using Javascript, you will learn how to add HTML elements using javascript, how to add some style to elements and how to get the input values.

this is the final result

Make the user interface

we will start by making the UI of our project using HTML and CSS, you can use the code below.
in this example, we have created an index.html file and main.css file and added it in the same folder.

index.html:
<!DOCTYPE HTML>
<html>
    
    <head>
        <title>To-Do List</title>
        <meta charset="utf-8">
        <link rel="stylesheet" href="main.css">
    </head>
    
    <body>
        <div class="container">
            <h1>To-do list</h1>
            
            <div class="add-elements">
                <input id="add" type="text" placeholder="add an element">
                <button id="btn">Add</button>
            </div>
            
            <div class="element-list">
                <ul id="list">
                    <li>buy some milk</li>
                    <li>do your home work</li>
                </ul>
            </div>
            
        </div>
        
        <script src="script.js"></script>
    </body>
    
</html>

and add the CSS code below 

main.css:
@import url('https://fonts.googleapis.com/css?family=Nunito&display=swap');
*{
    margin0;
    padding0;
    outline0;
}
body{
    background-color: #f5f5f5;
}

.container{
    position: absolute;
    left50%;
    top50%;
    transform: translate(-50%,-50%);
    background-color: #fafafa;
    box-shadow0 2px 8px rgba(0,0,0,.16);
    width460px;
    height680px;
    text-align: center;
    font-family'Nunito',sans-serif;
   
}
h1{
    margin12px 0;
}
.add-elements{
    margin12px 0;
    
}
input{
    padding6px 4px;
    width70%;
}
button{
    padding7px 14px;
    background-color: #00b0ff;
    border: none;
    border-radius4px;
    color: #fafafa;

}
.element-list{
    height550px;
    overflow-y: scroll
}
ul{
    padding15px;
   
}
li{
    list-style: none;
    padding10px 5px;
    background-color: #fff;
    margin12px 0;
    box-shadow0 2px 8px rgba(0,0,0,.16);
    border-radius3px;
    transition.4s;
}
button:hover{
    background-color: #1565c0;
}

li:hover{
    background-color: #e0e0e0;
}

.checked{
    text-decoration: line-through;
    background-color: #e0e0e0;
}


now let's begin with the javascript file

script.js:
const input = document.querySelector("#add");
const  btn = document.querySelector("#btn");
const list = document.querySelector("#list");
var el = document.getElementsByTagName('li');

// this function will allow us to add elements when we click the button
btn.onclick = function(){
    
    var txt = input.value;
    if(txt ==''){
        alert('you must write something');
    }else{
        li = document.createElement('li');
    li.innerHTML = txt;
    list.insertBefore(li,list.childNodes[0]);
    input.value = '';
    }
    
};

//this function will allow us to check the clicked elements
list.onclick = function(ev){
    if(ev.target.tagName == 'LI'){
         ev.target.classList.toggle('checked');
    }
};

and that's it, this is a basic to-do list app made in javascript, we can make it better by adding a storage file that will save all the list or you can use some database system like firebase to save your tasks and even if you refresh the page, you don't have to type all the task again and again.
Make a Todo list using JavaScript Make a Todo list using JavaScript Reviewed by Medics on October 19, 2019 Rating: 5

2 comments:

  1. doesn't working the code
    give the icons links and tasks are not add and not deleted

    ReplyDelete

-->
Powered by Blogger.