Skip to content
Youths Forum Youths Forum Youths Forum

Tech Blogs & Programming Tutorials

Youths Forum Youths Forum Youths Forum

Tech Blogs & Programming Tutorials

  • Blog
  • News
  • Programming
    • PHP
    • JavaScript
    • JQuery
    • CSS
    • HTML
    • API
  • Stock Market Live
  • Automobiles
    • Cars
  • Gadgets
    • Phones
    • Android Phones

Categories

  • Automobiles (12)
    • Cars (7)
  • Blog (103)
    • Poems (2)
    • Space (2)
  • Command (2)
  • Education (2)
  • Entertainment (4)
  • Gadgets (9)
    • Phones (8)
      • Android Phones (4)
  • HTML Templates (11)
  • IT Training Institutes (1)
  • Lifestyle (4)
  • News (51)
  • Others (23)
  • Programming (296)
    • API (16)
    • CSS (83)
    • Database (4)
    • Hosting (1)
    • HTML (37)
    • JavaScript (117)
      • JQuery (27)
      • ReactJS (7)
    • PHP (116)
  • Python (3)
  • recipes (1)
  • SEE Result (1)
  • Server (3)
  • Blog
  • News
  • Programming
    • PHP
    • JavaScript
    • JQuery
    • CSS
    • HTML
    • API
  • Stock Market Live
  • Automobiles
    • Cars
  • Gadgets
    • Phones
    • Android Phones
Close

Search

JavaScript

Create a To Do Application using JavaScript

By Admin
November 17, 2023 3 Min Read
Comments Off on Create a To Do Application using JavaScript

A to-do list is a simple and effective tool for managing tasks and organizing your day. It helps you outline and prioritize the things you need to accomplish, making it easier to stay focused and productive. The goal of a to-do list is to help you manage your time and tasks efficiently, reduce stress, and enhance productivity.

HTML

The HTML file begins with the usual document type declaration (<!DOCTYPE html>) and contains the basic structure of an HTML document.

The <head> section includes metadata like character set, viewport settings, and the title of the page.

The <body> section contains the main content of the application.

Follow the following video for complete guidance :

https://www.youtube.com/watch?v=9stcEPfjYyY&ab_channel=LetsTryThis%21

CSS

The CSS styles define the appearance of the to-do list application.
It sets the background color, font-family, and styling for the container, task list, task items, checkboxes, and delete buttons.

JavaScript

The JavaScript code is embedded within the <script> tag at the end of the HTML file.

Complete Source Code

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Todo List App</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f4f4f4;
        }

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

        #task-list {
            list-style-type: none;
            padding: 0;
        }

        .task-item {
            display: flex;
            justify-content: space-between;
            align-items: center;
            border-bottom: 1px solid #ddd;
            padding: 10px 0;
        }

        .task-item input[type="checkbox"] {
            margin-right: 10px;
        }

        .delete-btn {
            background-color: #e74c3c;
            color: #fff;
            border: none;
            padding: 5px 10px;
            cursor: pointer;
            border-radius: 3px;
        }
    </style>
</head>
<body>

<div id="todo-container">
    <h2>Todo List</h2>
    <input type="text" id="task-input" placeholder="Add a new task">
    <button onclick="addTask()">Add Task</button>
    <ul id="task-list"></ul>
</div>

<script>
    function addTask() {
        var taskInput = document.getElementById('task-input');
        var taskList = document.getElementById('task-list');

        if (taskInput.value.trim() === '') {
            alert('Please enter a task.');
            return;
        }

        var listItem = document.createElement('li');
        listItem.className = 'task-item';

        var checkbox = document.createElement('input');
        checkbox.type = 'checkbox';
        checkbox.addEventListener('change', function() {
            toggleTaskStatus(listItem);
        });

        var taskText = document.createTextNode(taskInput.value);

        var deleteButton = document.createElement('button');
        deleteButton.className = 'delete-btn';
        deleteButton.textContent = 'Delete';
        deleteButton.addEventListener('click', function() {
            deleteTask(listItem);
        });

        listItem.appendChild(checkbox);
        listItem.appendChild(taskText);
        listItem.appendChild(deleteButton);

        taskList.appendChild(listItem);

        taskInput.value = ''; // Clear the input field
    }

    function toggleTaskStatus(taskItem) {
        taskItem.classList.toggle('completed');
    }

    function deleteTask(taskItem) {
        taskItem.parentNode.removeChild(taskItem);
    }
</script>

</body>
</html>

 

Todo List Container

There is a container div with the id todo-container. It has a maximum width, a white background, padding, border-radius, and a box-shadow to give it a simple, card-like appearance.

Task Input and Buttons

The container includes an input field (task-input) for entering new tasks and a button (Add Task) to add tasks to the list.

Task List

The task list is represented by an unordered list (ul) with the id task-list.

Each task is represented as a list item (li) with the class task-item. Each list item contains a checkbox, the task text, and a delete button.

JavaScript Functions

addTask(): This function is called when the “Add Task” button is clicked. It creates a new list item, adds a checkbox, the task text, and a delete button. The new list item is then appended to the task list (ul).

toggleTaskStatus(taskItem): This function is called when the checkbox of a task is changed. It toggles the completed class on the task item, which changes its appearance to indicate completion.

deleteTask(taskItem): This function is called when the delete button of a task is clicked. It removes the corresponding task item from the list.

This simple to-do list application allows users to add tasks, mark them as completed using checkboxes, and delete tasks. It’s a good starting point for understanding the basics of HTML, CSS, and JavaScript interactions in a web application.

Tags:

featured
Author

Admin

Follow Me
Other Articles
Previous

Creating an Audio Recorder with JavaScript: A Step-by-Step Guide

Next

Netflix clone design using HTML and CSS

FIFA World Cup 2026 Predict and Win by SportsGuff

Recent Posts

  • Unpacking Nepal’s Record Rs 2.12 Trillion Budget and What It Means for You
  • How to Write a Strong Scholarship Application: The Ultimate Step-by-Step Guide
  • How to Prepare for Exams Without Stress: The Ultimate Science-Backed Guide
  • Chiranjibi Adhikari Appointed Acting President of CAN Federation
  • How to Design a Student Marksheet Using HTML and CSS

Tags

adsense ai animate animation animation using HTML and CSS API blog calculator chatgpt Cryptocurrency CSS css animation design Email Facebook featured filemanager file manager free template google htaccess HTML image Instagram interview javascript JQuery jquery ui NADA AutoShow NADA Auto Show 2024 password PHP Progressive Web App PWA QR random react reactjs Rotate travel Twitter vpn youthforum youthsforum youtube

About Us

At Youths Forum, we are passionate about sharing knowledge that empowers students, educators, professionals, and technology enthusiasts.

Our Mission

Our mission is simple: to make technology and education accessible, understandable, and beneficial for everyone. We strive to create content that helps our readers learn new skills and stay updated with industry developments.

RSS RSS

  • Unpacking Nepal’s Record Rs 2.12 Trillion Budget and What It Means for You Admin
  • How to Write a Strong Scholarship Application: The Ultimate Step-by-Step Guide Admin
  • How to Prepare for Exams Without Stress: The Ultimate Science-Backed Guide Admin

Quick Links

  • Stock Market Live
  • Parliament Election 2082
Copyright 2026 — Youths Forum. All rights reserved. Blogsy WordPress Theme