create a todo application in javascript

To create a simple todo application in JavaScript, you can follow the steps below:

  1. Create an HTML file that will contain the user interface of the todo application.
  2. Add a text input field for users to add new tasks, and a button to submit the task.
  3. Create an empty array that will hold the list of tasks.
  4. Use JavaScript to get the user input from the text input field when the user clicks the submit button.
  5. Add the new task to the array.
  6. Use JavaScript to display the array of tasks as a list on the browser.

Here's a sample implementation of the JavaScript code:

<!DOCTYPE html>
<html>
  <head>
    <title>Todo App</title>
  </head>
  <body>
    <h1>My Todo App</h1>
    <input type="text" id="taskInput" placeholder="Add new task...">
    <button onclick="addTask()">Add Task</button>
    <ul id="taskList">
    </ul>

    <script>
      let taskArray = [];

      function addTask() {
        // get user input
        let newTask = document.getElementById('taskInput').value;

        // add new task to array
        taskArray.push(newTask);

        // clear input field
        document.getElementById('taskInput').value = '';

        // display updated list of tasks
        displayTasks();
      }

      function displayTasks() {
        let taskList = document.getElementById('taskList');
        taskList.innerHTML = '';
        for (let i = 0; i < taskArray.length; i++){
          let listItem = document.createElement('li');
          listItem.appendChild(document.createTextNode(taskArray[i]));
          taskList.appendChild(listItem);
        }
      }
    </script>
  </body>
</html>
1040 chars
42 lines

In summary, the code above creates a simple todo application that allows users to add tasks and displays them in a list format.

gistlibby LogSnag