\n\n\n 💡 Explanation: #task-input: Where the user types the task. #add-task-btn: Button to add the task. #task-list: Where the tasks will appear as a list. 🎨 3. Basic CSS Styling (style.css) body {\n font-family: Arial, sans-serif;\n background-color: #f2f2f2;\n display: flex;\n justify-content: center;\n padding-top: 50px;\n}\n\n.container {\n background: white;\n padding: 30px;\n border-radius: 8px;\n box-shadow: 0 2px 10px rgba(0,0,0,0.1);\n width: 350px;\n}\n\n.input-section {\n display: flex;\n gap: 10px;\n margin-bottom: 20px;\n}\n\ninput[type=\"text\"] {\n flex: 1;\n padding: 10px;\n font-size: 16px;\n}\n\nbutton {\n padding: 10px;\n font-size: 16px;\n cursor: pointer;\n}\n\nul {\n list-style: none;\n padding: 0;\n}\n\nli {\n background-color: #eaeaea;\n margin-bottom: 10px;\n padding: 10px;\n display: flex;\n justify-content: space-between;\n align-items: center;\n}\n\nli.completed {\n text-decoration: line-through;\n color: #888;\n}\n\nbutton.delete {\n background: red;\n color: white;\n border: none;\n padding: 5px 10px;\n}\n\nbutton.complete {\n background: green;\n color: white;\n border: none;\n padding: 5px 10px;\n margin-right: 5px;\n}\n 💡 Explanation: Clean, modern styling for the input, buttons, and task list. .completed adds a strikethrough style when a task is marked done. ⚙️ 4. JavaScript Functionality (script.js) // Get elements\nconst addBtn = document.getElementById('add-task-btn');\nconst taskInput = document.getElementById('task-input');\nconst taskList = document.getElementById('task-list');\n\n// Add Task\naddBtn.addEventListener('click', () => {\n const taskText = taskInput.value.trim();\n\n if (taskText === '') {\n alert('Please enter a task!');\n return;\n }\n\n // Create list item\n const li = document.createElement('li');\n\n // Create task span\n const span = document.createElement('span');\n span.textContent = taskText;\n\n // Complete button\n const completeBtn = document.createElement('button');\n completeBtn.textContent = 'Complete';\n completeBtn.className = 'complete';\n\n completeBtn.addEventListener('click', () => {\n li.classList.toggle('completed');\n });\n\n // Delete button\n const deleteBtn = document.createElement('button');\n deleteBtn.textContent = 'Delete';\n deleteBtn.className = 'delete';\n\n deleteBtn.addEventListener('click', () => {\n taskList.removeChild(li);\n });\n\n // Append everything\n li.appendChild(span);\n li.appendChild(completeBtn);\n li.appendChild(deleteBtn);\n taskList.appendChild(li);\n\n // Clear input\n taskInput.value = '';\n});\n 💡 Explanation Step-by-Step: addEventListener('click', ...): Runs when \"Add Task\" is clicked. trim(): Removes spaces from start/end. createElement(): Dynamically creates HTML elements (like buttons). classList.toggle('completed'): Adds/removes the class to strike through text. removeChild(): Deletes the task item from the list. ✅ Features Summary Feature Description Add Task User types a task and clicks \"Add Task\" Complete Click “Complete” to mark task as done Delete Click “Delete” to remove the task 🧠 Final Tips You can improve this app by saving tasks to localStorage. Add a “clear all” button if needed. Try learning frameworks like React after mastering this!","@type":"NewsArticle","author":[{"@type":"Person","name":"ttimesnow","url":"https://www.ttimesnow.com/author/11"}],"description":"Learn how to build a simple and interactive to-do list app using HTML, CSS, and JavaScript. Includes adding, deleting, and marking tasks as complete with step-by-step code and explanations.","inLanguage":"en","dateModified":"2025-05-09T15:56:49.9Z","mainEntityOfPage":{"@type":"WebPage","@id":"https://www.ttimesnow.com/beginners-guide-to-building-a-javascript-todo-list-app-from-scratch"},"url":"https://www.ttimesnow.com/amp/beginners-guide-to-building-a-javascript-todo-list-app-from-scratch","datePublished":"2025-05-09T15:56:49.9Z","publisher":{"@type":"Organization","name":"TtimesNow","logo":{"@type":"ImageObject","width":180,"url":"https://www.ttimesnow.com/apple-touch-icon.png","height":180},"@id":"https://www.ttimesnow.com#organization","url":"https://www.ttimesnow.com","sameAs":["https://www.facebook.com/donotbelazymotivation"]},"@id":"https://www.ttimesnow.com/beginners-guide-to-building-a-javascript-todo-list-app-from-scratch#article","headline":"Beginner’s Guide to Building a JavaScript To-Do List App from Scratch","articleSection":"javascript","thumbnailUrl":"https://www.ttimesnow.com/view/gk/free-courses-to-learn-javascript.webp"}],"@context":"https://schema.org"}
Google Advertisement

Beginner’s Guide to Building a JavaScript To-Do List App from Scratch

Google Advertisement
🔥 Read with Full Features on Our Website

Learn how to build a simple and interactive to-do list app using HTML, CSS, and JavaScript. Includes adding, deleting, and marking tasks as complete with step-by-step code and explanations.

Published on 09 May 2025
By ttimesnow

📝 Create a To-Do List App with Add/Delete/Complete Features in JavaScript

🚀 Introduction

If you're new to JavaScript and looking for a small yet practical project, a To-Do List App is perfect! You'll learn how to:

Google Advertisement

Let’s dive in and build this app from scratch, step by step!


📁 1. Set Up Your Files

Create three files in your project folder:


🧱 2. HTML Structure (index.html)

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>My To-Do App</title>
  <link rel="stylesheet" href="style.css">
</head>
<body>
  <div class="container">
    <h1>To-Do List</h1>
    <div class="input-section">
      <input type="text" id="task-input" placeholder="Enter a new task...">
      <button id="add-task-btn">Add Task</button>
    </div>
    <ul id="task-list"></ul>
  </div>

  <script src="script.js"></script>
</body>
</html>

💡 Explanation:


🎨 3. Basic CSS Styling (style.css)

body {
  font-family: Arial, sans-serif;
  background-color: #f2f2f2;
  display: flex;
  justify-content: center;
  padding-top: 50px;
}

.container {
  background: white;
  padding: 30px;
  border-radius: 8px;
  box-shadow: 0 2px 10px rgba(0,0,0,0.1);
  width: 350px;
}

.input-section {
  display: flex;
  gap: 10px;
  margin-bottom: 20px;
}

input[type="text"] {
  flex: 1;
  padding: 10px;
  font-size: 16px;
}

button {
  padding: 10px;
  font-size: 16px;
  cursor: pointer;
}

ul {
  list-style: none;
  padding: 0;
}

li {
  background-color: #eaeaea;
  margin-bottom: 10px;
  padding: 10px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

li.completed {
  text-decoration: line-through;
  color: #888;
}

button.delete {
  background: red;
  color: white;
  border: none;
  padding: 5px 10px;
}

button.complete {
  background: green;
  color: white;
  border: none;
  padding: 5px 10px;
  margin-right: 5px;
}

💡 Explanation:


⚙️ 4. JavaScript Functionality (script.js)

// Get elements
const addBtn = document.getElementById('add-task-btn');
const taskInput = document.getElementById('task-input');
const taskList = document.getElementById('task-list');

// Add Task
addBtn.addEventListener('click', () => {
  const taskText = taskInput.value.trim();

  if (taskText === '') {
    alert('Please enter a task!');
    return;
  }

  // Create list item
  const li = document.createElement('li');

  // Create task span
  const span = document.createElement('span');
  span.textContent = taskText;

  // Complete button
  const completeBtn = document.createElement('button');
  completeBtn.textContent = 'Complete';
  completeBtn.className = 'complete';

  completeBtn.addEventListener('click', () => {
    li.classList.toggle('completed');
  });

  // Delete button
  const deleteBtn = document.createElement('button');
  deleteBtn.textContent = 'Delete';
  deleteBtn.className = 'delete';

  deleteBtn.addEventListener('click', () => {
    taskList.removeChild(li);
  });

  // Append everything
  li.appendChild(span);
  li.appendChild(completeBtn);
  li.appendChild(deleteBtn);
  taskList.appendChild(li);

  // Clear input
  taskInput.value = '';
});

💡 Explanation Step-by-Step:


✅ Features Summary

Feature Description
Add Task User types a task and clicks "Add Task"
Complete Click “Complete” to mark task as done
Delete Click “Delete” to remove the task

🧠 Final Tips

❤️ Like 💬 Comment 🔗 Share
Google Advertisement
👉 View Full Version on Main Website ↗
Google Advertisement
👉 Read Full Article on Website