,

Creating a Todo List App with JavaScript: A Beginner’s Tutorial on Using Local Storage

Posted by






Todo List App

Creating a Todo List App using JavaScript

In this tutorial, we will be creating a simple Todo List app using JavaScript. We will also be using local-storage to store the todo items so that they persist even after the browser is closed.

HTML

First, let’s set up the HTML structure for our todo list app. We will have an input field to add new todo items, a button to add the item, and a list to display the items.

        
            <input type="text" id="todoInput" placeholder="Enter a new todo">
            <button onclick="addTodo()">Add</button>
            <ul id="todoList"></ul>
        
    

JavaScript

Next, we will write the JavaScript code to handle adding todo items and using local-storage to save and retrieve the items.

        
            function addTodo() {
                var input = document.getElementById('todoInput').value;
                if (input === '') {
                    alert('Please enter a todo item');
                } else {
                    var todoList = document.getElementById('todoList');
                    var li = document.createElement('li');
                    li.appendChild(document.createTextNode(input));
                    todoList.appendChild(li);
                    saveTodo(input);
                }
            }

            function saveTodo(todo) {
                var todos = JSON.parse(localStorage.getItem('todos')) || [];
                todos.push(todo);
                localStorage.setItem('todos', JSON.stringify(todos));
            }

            function loadTodos() {
                var todos = JSON.parse(localStorage.getItem('todos')) || [];
                var todoList = document.getElementById('todoList');
                todos.forEach(function(todo) {
                    var li = document.createElement('li');
                    li.appendChild(document.createTextNode(todo));
                    todoList.appendChild(li);
                });
            }

            loadTodos();
        
    

Local Storage

We are using the localStorage API to save and retrieve the todo items. The data is stored as a JSON string, so we use JSON.parse() and JSON.stringify() to convert the data to and from a string.

Conclusion

With just a few lines of JavaScript code, we have created a simple todo list app that uses local-storage to persist the todo items. This is a great example of how powerful JavaScript can be for creating interactive and dynamic web applications.


0 0 votes
Article Rating
10 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Nepali honi
7 months ago

Great Explanation

common man
7 months ago

how to add remainder alert in this app

Mihir kumar
7 months ago

Please a project where we can learn some advance angular topics and we can put it in our resume

DCode
7 months ago

Thank you for sharing this video👍🏻

voidChetan2
7 months ago

Thanks sir for all efforts. No one provide this kond of Knowledge that you provide. Keep it up

Sanket
7 months ago

Good one .can u complete remaining functionality with filter

Defaulter Gaming
7 months ago

Pls ecommerce websites

eddie
7 months ago

very basic. Didn't know about adding event also

AKANKSHA ASUTKAR
7 months ago

Nice n easy

apps games
7 months ago

Make same tutorial using jQuery