MC, 2025
Ilustracja do artykułu: How to Make a To-Do App in JavaScript: A Beginner's Guide

How to Make a To-Do App in JavaScript: A Beginner's Guide

Creating a to-do app is one of the most popular projects for beginners in JavaScript. It’s simple, yet powerful enough to demonstrate key concepts such as DOM manipulation, event handling, and local storage. In this guide, we'll walk you through the steps of building your very own to-do app using JavaScript. Ready to get started? Let’s dive in!

Why Build a To-Do App?

A to-do app is an excellent project for anyone looking to learn JavaScript. It’s a simple yet highly functional application that can help you understand how to handle user inputs, work with arrays, and update the DOM dynamically. Additionally, building a to-do app introduces you to local storage, which allows data to persist even after refreshing the page. Plus, it’s always nice to have a working to-do list that you can actually use!

What You'll Need

Before we begin, here’s a quick rundown of what you’ll need to build your to-do app:

  • Basic knowledge of HTML, CSS, and JavaScript: You should be familiar with the basic syntax and structure of these languages.
  • A text editor: You can use any text editor like Visual Studio Code, Sublime Text, or Atom to write your code.
  • A browser: You’ll need a browser to run and test your app. Chrome or Firefox works great for this purpose.

Step 1: Set Up the HTML Structure

The first step in building any web app is creating the structure. Let’s start by creating a simple HTML file. This file will contain a basic layout for our to-do app.




  
  
  To-Do App
  


  

To-Do App

    In this HTML, we have a simple input field for entering tasks, a button to add tasks, and an unordered list (`

      `) where the tasks will appear. The JavaScript file (`app.js`) and the CSS file (`styles.css`) are also linked.

      Step 2: Style the App with CSS

      Next, let’s add some basic styles to make our app look nice. Create a file called `styles.css` and add the following code:

      * {
        margin: 0;
        padding: 0;
        box-sizing: border-box;
      }
      
      body {
        font-family: Arial, sans-serif;
        background-color: #f4f4f4;
        display: flex;
        justify-content: center;
        align-items: center;
        height: 100vh;
      }
      
      .container {
        background-color: white;
        padding: 20px;
        border-radius: 8px;
        box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
      }
      
      input[type="text"] {
        padding: 10px;
        width: 300px;
        border: 2px solid #ccc;
        border-radius: 5px;
        margin-right: 10px;
      }
      
      button {
        padding: 10px 20px;
        background-color: #4CAF50;
        color: white;
        border: none;
        border-radius: 5px;
        cursor: pointer;
      }
      
      button:hover {
        background-color: #45a049;
      }
      
      ul {
        margin-top: 20px;
        list-style-type: none;
      }
      
      li {
        background-color: #e7e7e7;
        padding: 10px;
        margin-bottom: 10px;
        border-radius: 5px;
      }
      
      li button {
        background-color: red;
        color: white;
        border: none;
        border-radius: 5px;
        cursor: pointer;
      }
      
      li button:hover {
        background-color: #cc0000;
      }
      

      In this CSS, we’ve set up some basic styling to make the app look clean and user-friendly. Feel free to customize the styles further to match your preferences!

      Step 3: Add Functionality with JavaScript

      Now comes the fun part: adding functionality with JavaScript! We’ll write the code to allow users to add tasks, remove tasks, and persist tasks even after the page is refreshed.

      // app.js
      
      // Get elements from the DOM
      const newTaskInput = document.getElementById('new-task');
      const addTaskButton = document.getElementById('add-task');
      const taskList = document.getElementById('task-list');
      
      // Function to add a new task
      function addTask() {
        const taskText = newTaskInput.value.trim();
        if (taskText === '') {
          return;
        }
      
        const taskItem = document.createElement('li');
        taskItem.textContent = taskText;
      
        const removeButton = document.createElement('button');
        removeButton.textContent = 'Remove';
        removeButton.onclick = () => {
          taskItem.remove();
          saveTasks();
        };
      
        taskItem.appendChild(removeButton);
        taskList.appendChild(taskItem);
      
        newTaskInput.value = ''; // Clear the input field
      
        saveTasks(); // Save tasks to local storage
      }
      
      // Function to save tasks to local storage
      function saveTasks() {
        const tasks = [];
        const taskItems = taskList.getElementsByTagName('li');
        for (let task of taskItems) {
          tasks.push(task.textContent.replace('Remove', '').trim());
        }
        localStorage.setItem('tasks', JSON.stringify(tasks));
      }
      
      // Function to load tasks from local storage
      function loadTasks() {
        const tasks = JSON.parse(localStorage.getItem('tasks')) || [];
        tasks.forEach(task => {
          const taskItem = document.createElement('li');
          taskItem.textContent = task;
      
          const removeButton = document.createElement('button');
          removeButton.textContent = 'Remove';
          removeButton.onclick = () => {
            taskItem.remove();
            saveTasks();
          };
      
          taskItem.appendChild(removeButton);
          taskList.appendChild(taskItem);
        });
      }
      
      // Event listener to add task when button is clicked
      addTaskButton.addEventListener('click', addTask);
      
      // Load saved tasks when the page loads
      window.onload = loadTasks;
      

      In this JavaScript code, we:

      • Get references to the DOM elements: the input field, the button, and the task list.
      • Create a function to add a new task by dynamically creating an `
      • ` element and appending it to the task list.
      • Create a function to save tasks to local storage so that they persist after a page reload.
      • Create a function to load tasks from local storage when the page is first loaded.
      • Add an event listener to the "Add Task" button to trigger the task-adding functionality.

      Step 4: Test Your App!

      Now that we’ve set up everything, it’s time to test your to-do app! Open the `index.html` file in your browser and try adding some tasks. You should see them appear in the list, and you can remove them by clicking the “Remove” button. Refresh the page, and your tasks should still be there, thanks to local storage!

      Conclusion

      Congratulations! You’ve just built a fully functional to-do app using JavaScript. This project has introduced you to key concepts like DOM manipulation, event handling, and local storage, all of which are fundamental skills for any web developer. Of course, there’s always room for improvement, so feel free to experiment with adding new features, such as task editing, due dates, or categorization. Happy coding!

    Komentarze (0) - Nikt jeszcze nie komentował - bądź pierwszy!

    Imię:
    Treść: