Create a simple to-do list application using HTML, CSS, and JavaScript

Create a simple to-do list application using HTML, CSS, and JavaScript

Practical Question: To-Do List Application

Create a simple to-do list application using HTML, CSS, and JavaScript. The application should allow users to:

  1. Add new to-do items.
  2. Mark items as completed.
  3. Remove items from the list.
  4. Persist the to-do list in the browser’s local storage so that the list remains after refreshing the page.

Solution

Step 1: Create the HTML structure

  • Head Section: Contains metadata, the title of the document, and a link to the CSS file for styling.
  • Body Section: Includes a container div with an input field for new tasks, a button to add tasks, and an unordered list to display the tasks.

Step 2: Add CSS for styling

  • Body Styling: Centers the content on the page and sets a background color.
  • Container Styling: Styles the main container with padding, background color, border radius, and shadow.
  • Input and Button Styling: Styles the input field and button for adding tasks.
  • List and List Item Styling: Styles the list and its items, including a class for completed tasks.

Step 3: Write JavaScript for functionality

Explanation

Detailed Explanation

  1. Select DOM elements:
    • newTodoInput, addBtn, and todoList are selected using getElementById to manipulate them later.
  2. Load existing todos from localStorage:
    • When the DOM content is loaded, loadTodos function is called to populate the list with any previously saved tasks.
  3. Add new todo:
    • addBtn.addEventListener('click', addTodo) adds a click event listener to the “Add” button, calling addTodo function when clicked.
  4. Add todo function:
    • addTodo() function gets the text from the input field, trims it, and checks if it’s not empty.
    • If the input is valid, it calls createTodoItem(todoText) to create a new list item and appends it to the todoList.
    • It then saves the new task to localStorage and clears the input field.
  5. Create todo item element:
    • createTodoItem(todoText) creates a new li element for the task.
    • It creates “Complete” and “Remove” buttons and adds click event listeners to toggle the completed class and remove the item, respectively.
    • The buttons are appended to the li element, which is then returned.
  6. Save todo to localStorage:
    • saveTodoToLocalStorage(todoText) retrieves the current list of todos from localStorage (or initializes an empty array if none exist).
    • It adds the new task to the array and saves it back to localStorage.
  7. Remove todo from localStorage:
    • removeTodoFromLocalStorage(todoText) retrieves the current list of todos from localStorage.
    • It filters out the task to be removed and saves the updated array back to localStorage.
  8. Load todos from localStorage:
    • loadTodos() retrieves the list of todos from localStorage and creates a list item for each saved task, appending it to the todoList.

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *