Creating a task management system using HTML, CSS, and JavaScript that utilizes local

I have been extensively researching how to create a to-do list using JavaScript with local storage, but unfortunately, I have not been able to find exactly what I am looking for.

Is there a way for me to set up a to-do list and input data before actually entering any tasks? The image below shows that currently no tasks have been added:

https://i.sstatic.net/6KYxg.png

Answer №1

To begin, retrieve the data from the localStorage. In the demonstration provided, a data array is used because localStorage cannot be utilized on Stackoverflow. After that, loop through the array and generate a new list element for each item, then add it to your todo list.

example in action

const form = document.querySelector("form");
const input = document.querySelector("#input");
const list = document.querySelector("ul");
const data = ["task 1", "task 2"];

function init() {
  data.forEach(e => {
    let li = document.createElement('li');
    li.innerHTML = e;
    const delBtn = document.createElement("button");
    delBtn.innerText = 'x'
    li.append(' ',delBtn);
    list.append(li)

  })
}
init();
///

form.addEventListener("submit", function(e) {
  e.preventDefault();
  toDoLine();
  input.value = "";
});

list.addEventListener('click', function(e){
  if (e.target.nodeName === 'BUTTON') {
      e.target.closest('LI').remove();
  }
})


function toDoLine() {
  if (input.value !== '') {
  const toDo = input.value;
  const newLi = document.createElement("li");
  newLi.innerText = toDo;
  const delBtn = document.createElement("button");
  delBtn.innerText = 'x'
  newLi.append(' ', delBtn);
  list.append(newLi);
    } else {
      alert('add something to the list!')
    }
  }
body {
  text-align: center;
}
#list {
  display: inline-block;  
}

li {
  text-align: left;
}
<h1>To Do List</h1>
<div>
  <form>
    <input action ='/test' id="input" type="text" placeholder="task">
    <button id='submit'>submit</button>
  </form> 
</div>
<div id='listdiv'>
    <ul id="list"></ul>
</div>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Automatic Refresh and Search Functionality on PHP Page

Can anyone help with a problem I'm having trying to implement auto-reload on a page with a table in the form of rows that contain usernames? I am currently using the jQuery searchable plugin from here to search for usernames within the div. The websi ...

Inject pure HTML code into a react div

I have extracted raw HTML content stored in the database and I am attempting to display it within a div element. const style = { border: '2px solid #eee', height: '270px', width: '100%', overflow: 'auto&ap ...

Is there a way to adjust the background hues of a React component when a click event is triggered?

Currently, I have 3 React components that each consist of a div element. When I click on one component, I want to change its background color to black. If I then select another component, the previously selected component should return to its normal back ...

To dismiss the Div, simply click on any area outside of it. Leveraging the power of SVG, D3

I need a way to hide my div by clicking outside of it. My SVG has a background and a graph with nodes on top of that. I have a special node (circle) on the graph, clicking on which makes a box appear. To show the box, I use the following code: d3.select ...

Inability to successfully upload batch data within specified criteria using a keyword and conditional statement

My goal is to batch the data, using "Repair" as a separator for the data. Splitting criteria = Repair Copper limit = 2.5 [ {"engineSN":"20","timeRun":"30","Cu":"2"}, {"engineSN": ...

Storing customer information securely on the server with the help of Node.js

After spending some time experimenting with Node.js on my local machine, I've realized that my understanding of HTTP requests and XHR objects is quite limited. One particular challenge I've encountered while using Node is figuring out how to effe ...

I encountered a height discrepancy when attempting to style an unordered list to resemble a table

I have created a set of ul lists that appear as a 2-column table, just as I needed. However, an issue arises when the content of any list increases. The problem is that equal height is not being applied, causing the table to look broken. Here is my Fiddle ...

Get rid of the add to cart message and modify the button that says "add to cart" on Woocommerce

How can I modify the shopping cart page to remove the "has been added to your cart" text and replace the quantity and add to cart button with a custom button (my image)? Here is an example of what I am trying to achieve: This is the current state of the p ...

Is there a more efficient method than creating a separate variable for the navbar on each individual page where it is being utilized?

Apologies for the unclear title, I struggled to find the right wording and decided it would be easier to illustrate with code. Let's assume I have the following routes: router.get('/chest', (req, res)=>res.render('muscles/chest/chest ...

Is there a way for me to use an ajax xmlhttprequest to submit all the selected values from my radio buttons?

Here's the situation: I have four input forms [A-D] and I have selected options A and B. I want to transfer this information to another page, but I'm not sure how to do it. I've tried searching for tutorials on AJAX, but most of them focus ...

Using Javascript to dynamically retrieve accordion content from a dynamically generated ID

I am currently working on developing a more intricate accordion feature. The accordion will consist of 4 buttons (section 0-3) with unique content (lorem ipsum 0-3). Clicking on "section 0" should reveal "lorem ipsum 0", while clicking on "section 1" shoul ...

JavaScript libraries are not required when using the .append function to add HTML elements

Currently, I am attempting to utilize $.ajax in order to retrieve an html string from a php file and append it to the current html div. Oddly enough, when I use php echo, everything functions properly. However, when I attempt to load dynamically using $.lo ...

JavaScript EasyBitConverter

Looking to create a basic C# BitConverter equivalent in JavaScript, I've developed a simple BitConverter implementation. class MyBitConverter { constructor() {} GetBytes(int) { var b = new Buffer(8); b[0] = int; b ...

Installing global confusion with NPM

npm install css-sprite --save npm install css-sprite -g I'm curious about the significance of these two commands. I understand that "-g" makes it global, but why is that important? And what does "--save" do exactly? ...

Having an issue with ng-model not functioning correctly in md-menu with AngularJS material

I'm currently using md-menu from Angular Material and I have integrated a search box with ng-model in the list. However, I am facing an issue where I cannot access ng-model because 'md-menu-content' is out of scope. Here is my code: <div ...

Adjusting the background width results in the container position being altered as well

Currently, I am working with two background images, each accompanied by a red box positioned absolutely inside a bootstrap container. The first red box is located on the right side and the second red box is on the left side. So far, everything is running ...

Send form data using ajax technology

When testing my code on localhost, everything runs smoothly. However, when I tried running it on the server, it doesn't seem to be functioning properly. $("#MyUploadForm").ajaxSubmit(options); I would greatly appreciate any assistance with t ...

Submitting data with ajax in MVC when an option is chosen in a dropdown menu

Within my form, I have multiple dropdown lists. Whenever a user selects an option from one of these dropdowns, I want that value to be saved in the backend database. To avoid reloading the page, I believe using Ajax is the best approach, but I need assista ...

Adjusting the transparency of each segment within a THREE.LineSegments object

I am following up on a question about passing a color array for segments to THREE.LineSegments, but I am looking for a solution that does not involve low-level shaders. I am not familiar with shaders at all, so I would prefer to avoid them if possible. I ...

Reactjs is failing to display the page

I am facing an issue where the components are not rendering in the browser even though there is no error being displayed. This makes it difficult to troubleshoot and resolve the problem. Can someone help me identify the root cause? import React, { Com ...