Skip to main content

Building an Advanced To-Do List with HTML, CSS, and JavaScript

KNOW MORE :- https://codexdindia.blogspot.com/2024/02/building-advanced-to-do-list-with-html.html

Introduction

In the world of web development, understanding how to create interactive and dynamic user interfaces is crucial. In this article, we'll walk through the process of building an advanced to-do list using HTML, CSS, and JavaScript. This project will not only help you grasp the fundamentals of these technologies but also introduce more advanced concepts like local storage. Let's dive in!


Setting Up the HTML Structure

The foundation of our to-do list lies in the HTML structure. We create a simple webpage with a container, an unordered list to hold tasks, and input fields for adding tasks and notes. Each task in the list will consist of a checkbox, the task itself, and a delete button.

<!-- Sample HTML Structure -->
<div id="todo-container">
  <h2>Advanced To-Do List</h2>
  <ul id="task-list"></ul>
  <input type="text" id="add-task" placeholder="Add a new task">
  <button onclick="addTask()">Add Task</button>
  <input type="text" id="add-note" placeholder="Add a note">
  <button onclick="addNote()">Add Note</button>
  <button id="clear-completed" onclick="clearCompleted()">Clear Completed</button>
</div>

Styling with CSS

Adding styles enhances the user experience. Our to-do list will have a clean and user-friendly design, achieved using basic CSS styling. The styling includes a centered container, task formatting, and buttons for actions.

/* Sample CSS Styles */
body {
  font-family: 'Arial', sans-serif;
  background-color: #f4f4f4;
  text-align: center;
  margin-top: 50px;
}

#todo-container {
  width: 300px;
  margin: 0 auto;
  background-color: #fff;
  padding: 20px;
  border-radius: 5px;
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}

/* ... (Other styles for tasks, buttons, etc.) ... */

Implementing JavaScript Functionality

The real magic happens with JavaScript. We handle user interactions, dynamically create HTML elements, and manage local storage to persist tasks even after a page refresh.

// Sample JavaScript Functions
const taskList = document.getElementById('task-list');
const addTaskInput = document.getElementById('add-task');
const addNoteInput = document.getElementById('add-note');

function addTask() {
  const taskText = addTaskInput.value.trim();

  if (taskText !== '') {
    const taskItem = createTaskItem(taskText);
    taskList.appendChild(taskItem);

    addTaskInput.value = '';

    // Save tasks to local storage
    saveTasks();
  }
}

function addNote() {
  const noteText = addNoteInput.value.trim();

  if (noteText !== '') {
    const noteItem = createNoteItem(noteText);
    taskList.appendChild(noteItem);

    addNoteInput.value = '';

    // Save tasks to local storage
    saveTasks();
  }
}

function toggleTaskCompletion(event) {
  const taskItem = event.target.parentNode;
  taskItem.classList.toggle('completed');

  // Save tasks to local storage
  saveTasks();
}

function deleteTask(event) {
  const taskItem = event.target.parentNode;
  taskList.removeChild(taskItem);

  // Save tasks to local storage
  saveTasks();
}

function clearCompleted() {
  const completedTasks = document.querySelectorAll('.completed');
  completedTasks.forEach(taskItem => {
    taskList.removeChild(taskItem);
  });

  // Save tasks to local storage
  saveTasks();
}

function saveTasks() {
  const tasks = [];
  const taskItems = document.querySelectorAll('.task');

  taskItems.forEach(taskItem => {
    const taskText = taskItem.querySelector('span').textContent;
    const isCompleted = taskItem.classList.contains('completed');
    tasks.push({ text: taskText, completed: isCompleted });
  });

  localStorage.setItem('tasks', JSON.stringify(tasks));
}

function loadTasks() {
  const storedTasks = localStorage.getItem('tasks');
  if (storedTasks) {
    const tasks = JSON.parse(storedTasks);

    tasks.forEach(task => {
      const taskItem = task.completed ? createTaskItem(task.text) : createNoteItem(task.text);
      if (task.completed) {
        taskItem.classList.add('completed');
      }
      taskList.appendChild(taskItem);
    });
  }
}

function createTaskItem(text) {
  const taskItem = document.createElement('li');
  taskItem.classList.add('task');

  const checkbox = document.createElement('input');
  checkbox.type = 'checkbox';
  checkbox.addEventListener('change', toggleTaskCompletion);

  const taskTextElement = document.createElement('span');
  taskTextElement.textContent = text;

  const deleteButton = document.createElement('button');
  deleteButton.textContent = 'Delete';
  deleteButton.addEventListener('click', deleteTask);

  taskItem.appendChild(checkbox);
  taskItem.appendChild(taskTextElement);
  taskItem.appendChild(deleteButton);

  return taskItem;
}

function createNoteItem(text) {
  const noteItem = document.createElement('li');
  noteItem.textContent = text;
  noteItem.classList.add('note');
  return noteItem;
}

// Load tasks from local storage when the page is loaded
document.addEventListener('DOMContentLoaded', loadTasks);

Building Functionality Step by Step

Adding Tasks

The addTask function takes the input value, creates a task item dynamically, and appends it to the task list. We also save the updated task list to local storage.

Adding Notes

Similar to adding tasks, the addNote function creates a note item and appends it to the task list. Notes don't have checkboxes and completion status, providing a clear distinction from tasks.

Toggling Task Completion

The toggleTaskCompletion function responds to checkbox changes, marking tasks as completed or incomplete. Again, the updated list is saved to local storage.

Deleting Tasks

The deleteTask function removes a task or note when the delete button is clicked. The modified list is then saved to local storage.

Clearing Completed Tasks

The clearCompleted function removes all completed tasks, offering users a quick way to tidy up their list.

Saving and Loading Tasks from Local Storage

To persist tasks across page reloads, we use local storage. The saveTasks function stores the current task list, while loadTasks retrieves and displays the saved tasks when the page loads.


Conclusion

Building an advanced to-do list involves integrating HTML, CSS, and JavaScript to create a seamless user experience. By implementing features like task completion, deletion, and local storage, you've gained valuable insights into web development concepts. Continue exploring and applying these principles to more complex projects to solidify your understanding and skills. Happy coding!

Comments

Popular posts from this blog

How to Add a VS Code Editor to Your Website

How to Add a VS Code Editor to Your Website The Monaco editor by Microsoft provides a code editor component that can be easily integrated into websites. With just a few lines of code, you can add a full-featured editor similar to VS Code in your web app. In this tutorial, we'll see how to do just that. Getting Started To use Monaco, we need to include it in our page. We can get it from a CDN: < script src = "https://cdnjs.cloudflare.com/ajax/libs/monaco-editor/0.23.0/min/vs/loader.min.js" > </ script > This will load the Monaco library asynchronously. Next, we need a <div> in our HTML where we can instantiate the editor: < div id = "editor" ></ div > Now in our JavaScript code, we can initialize Monaco and create the editor: require .config({ paths : { 'vs' : 'https://cdnjs.cloudflare.com/ajax/libs/monaco-editor/0.23.0/min/vs' }}); require ([ "vs/editor/editor.main" ], function ( ) { const ...

10 Free GitHub Copilot Alternatives for VS Code in 2024

10 Free GitHub Copilot Alternatives for VS Code in 2024 As developers, we're always on the lookout for tools that can help us write code more efficiently. GitHub Copilot has been a game-changer in this regard, but its premium pricing may be a deterrent for some. Fortunately, there are several free alternatives available that offer similar functionality. In this article, we'll explore 10 of the best free GitHub Copilot alternatives for Visual Studio Code in 2024. Comparison of Free GitHub Copilot Alternatives Tool Language Support Auto-Completion Code Generation Code Explanation Bito Python, JavaScript, TypeScript, Java, C#, C++, Go, Ruby, PHP, Swift, Kotlin, Rust, Scala ✓ ✓ ✓ Tabnine Python, JavaScript, TypeScript, Java, C#, C++, Go, Ruby, PHP, Swift, Kotlin, Rust, Scala ✓ ✓ ✗ Amazon CodeWhisperer Python, JavaScript, TypeScript, Java, C#, C++, Go, Ruby, PHP ✓ ✓ ✗ Codeium Python, JavaScript, TypeScript, Java, C#, C...

Top React UI Libraries ๐ŸŒŸ

๐ŸŒŸ The Ultimate Roundup of Top React UI Libraries for Your Next Project! ๐Ÿš€๐ŸŽจ Hey there, React wizards! ๐Ÿช„✨ Ready to take your frontend game to the next level? Let's dive into an even broader spectrum of incredible React UI libraries that'll make your interfaces shine like never before! ๐Ÿ’ป๐ŸŒˆ 1. Tremor UI ๐ŸŒŠ ๐ŸŒŸ Tremor UI is a rising star in the React UI galaxy! ✨ It offers a sleek and modern design language, perfect for crafting stylish buttons and more. ๐Ÿ”˜๐ŸŽจ With Tremor, you can effortlessly create eye-catching user interfaces with its intuitive API and customizable components. ๐Ÿช„✨ Key Features : Modern Design Aesthetic Easy Customization Focus on User Experience 2. Radix UI ๐ŸŒฑ ๐ŸŒŸ Radix UI is all about building accessible, powerful components for React. ๐Ÿ› ️๐Ÿ”ฉ From modals to tooltips, Radix UI provides a solid foundation for creating interactive and user-friendly interfaces. ๐ŸŒ๐Ÿงก Dive into Radix ...

Random Posts