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>