How to add checked To-do to the local storage, so that when I refresh, the to-do stays checked?
//? Selectors
const todoInput = document.querySelector('.todo-input');
const todoButton = document.querySelector('.todo-button');
const todoList = document.querySelector('.todo-list');
const filterOption = document.querySelector('.filter-todo');
//! Listeners
document.addEventListener('DOMContentLoaded', getTodos);
todoButton.addEventListener('click', addTodo);
todoList.addEventListener('click', deleteCheck);
filterOption.addEventListener('click', filterTodo);
//TODO Functions
function addTodo(event) {
//Prevent from submitting
event.preventDefault()
//.Todo DIV
const todoDiv = document.createElement('div');
todoDiv.classList.add('todo');
//Creat LI
const newTodo = document.createElement('li');
newTodo.innerHTML = todoInput.value;
newTodo.classList.add('todo-item');
todoDiv.appendChild(newTodo);
//ADD TODO TO LOCALSTORAGE
saveLocalTodos(todoInput.value);
//CHECK MARK BUTTON
const completedButton = document.createElement('button');
completedButton.innerHTML = '<i class="fas fa-check"></i>';
completedButton.classList.add('completed-button');
todoDiv.appendChild(completedButton);
//CHECK TRASH BUTTON
const trashButton = document.createElement('button');
trashButton.innerHTML = '<i class="fas fa-trash"></i>';
trashButton.classList.add('trash-button');
todoDiv.appendChild(trashButton);
//APPEND TO LIST
todoList.appendChild(todoDiv);
//Clear Todo INPUT VALUE
todoInput.value = '';
}
function deleteCheck(e) {
const item = e.target;
//DELETE TODO
if (item.classList[0] === 'trash-button') {
const todo = item.parentElement;
//Animation
todo.classList.add("fall");
removeLocalStorge(todo);
todo.addEventListener('transitionend', function() {
todo.remove();
});
}
//CHECK MARK
if (item.classList[0] === "completed-button") {
const todo = item.parentElement;
todo.classList.toggle('completed');
}
}
function filterTodo(event) {
const todos = todoList.childNodes;
todos.forEach(function(todo) {
const mStyle = todo.style;
if (mStyle != undefined && mStyle != null) {
switch (event.target.value) {
case 'all':
todo.style.display = "flex";
break;
case "completed":
if (todo.classList.contains("completed")) {
todo.style.display = "flex";
} else {
todo.style.display = "none";
}
break;
case "uncompleted":
if (!todo.classList.contains("completed")) {
todo.style.display = "flex"
} else {
todo.style.display = "none";
}
break;
}
}
});
}
function saveLocalTodos(todo) {
//CHECK
let todos;
if (localStorage.getItem("todos") === null) {
todos = [];
} else {
todos = JSON.parse(localStorage.getItem("todos"));
}
todos.push(todo);
localStorage.setItem("todos", JSON.stringify(todos));
}
function getTodos() {
let todos;
if (localStorage.getItem("todos") === null) {
todos = [];
} else {
todos = JSON.parse(localStorage.getItem("todos"));
}
todos.forEach(function(todo) {
const todoDiv = document.createElement('div');
todoDiv.classList.add('todo');
//Creat LI
const newTodo = document.createElement('li');
newTodo.innerHTML = todo;
newTodo.classList.add('todo-item');
todoDiv.appendChild(newTodo);
//CHECK MARK BUTTON
const completedButton = document.createElement('button');
completedButton.innerHTML = '<i class="fas fa-check"></i>';
completedButton.classList.add('completed-button');
todoDiv.appendChild(completedButton);
//CHECK TRASH BUTTON
const trashButton = document.createElement('button');
trashButton.innerHTML = '<i class="fas fa-trash"></i>';
trashButton.classList.add('trash-button');
todoDiv.appendChild(trashButton);
//APPEND TO LIST
todoList.appendChild(todoDiv);
});
}
function removeLocalStorge(todo) {
let todos;
if (localStorage.getItem("todos") === null) {
todos = [];
} else {
todos = JSON.parse(localStorage.getItem("todos"));
}
const todoIndex = todo.children[0].innerText;
todos.splice(todos.indexOf(todoIndex), 1);
localStorage.setItem('todos', JSON.stringify(todos));
}
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
background-image: linear-gradient(120deg, #f6d365, #ff8e6f);
color: white;
font-family: 'poppins', sans-serif;
min-height: 100vh;
}
header {
font-size: 1.5rem;
}
header,
form {
min-height: 20vh;
display: flex;
justify-content: center;
align-items: center;
}
form input {
border-radius: 10px 0px 0px 10px;
}
form button {
border-radius: 0px 10px 10px 0px;
}
form input,
form button {
padding: 0.5rem;
font-size: 2rem;
border: none;
outline: none;
background: white;
}
form button {
color: #ff8e6f;
background: white;
cursor: pointer;
transition: all 0.3s ease;
}
form button:hover {
background-color: #ff8e6f;
color: white;
}
.todo-container {
display: flex;
justify-content: center;
align-items: center;
}
.todo-list {
min-width: 30%;
list-style: none;
}
.todo {
margin: 0.5rem;
background: white;
color: black;
font-size: 1.5rem;
border-radius: 10px 10px 10px 10px;
display: flex;
justify-content: space-between;
align-items: center;
transition: all 0.5s ease;
}
.todo li {
flex: 1;
}
.trash-button,
.completed-button {
background: #ff6f47;
color: white;
border: none;
padding: 1rem;
cursor: pointer;
font-size: 1rem;
}
.trash-button {
border-radius: 0px 10px 10px 0px;
}
.completed-button {
background: rgb(73, 204, 73);
}
.todo-item {
padding: 0rem 0.5rem;
}
.fa-trash,
.fa-check {
pointer-events: none;
}
.completed {
text-decoration: line-through;
opacity: 0.5;
}
.fall {
transform: translateY(8rem) rotateZ(20deg);
opacity: 0;
}
select {
-webkit-appearance: none;
-moz-appearance: none;
appearance: none;
outline: none;
box-shadow: none;
border: none;
border-radius: 10px;
}
.select {
margin: 1rem;
position: relative;
overflow: hidden;
}
select {
color: #ff6f47;
width: 10rem;
cursor: pointer;
padding: 1rem;
}
.select::after {
content: "\25BC";
position: absolute;
background: #ff6f47;
top: 0;
right: 0;
padding: 1rem;
border-radius: 0 10px 10px 0;
pointer-events: none;
transition: all 0.3s ease;
}
.select:hover::after {
background: white;
color: #ff6f47;
}
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="preconnect" href="https://fonts.googleapis.com">
<link href="https://fonts.googleapis.com/css2?family=Poppins&display=swap" rel="stylesheet">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.1.2/css/all.min.css" integrity="sha512-1sCRPdkRXhBV2PBLUdRb4tMg1w2YPf37qatUFeS7zlBy7jJI8Lf4VHwWfZZfpXtYSLy85pkm9GaYVYMfw5BC1A==" crossorigin="anonymous" referrerpolicy="no-referrer"
/>
<link rel="stylesheet" href="./style.css">
<title>Todo-List</title>
</head>
<body>
<header>
<h1>Yamen's Todo List</h1>
</header>
<form>
<input type="text" class="todo-input">
<button class="todo-button" type="submit">
<i class="fas fa-plus-square"></i>
</button>
<div class="select">
<select name="todos" class="filter-todo">
<option value="all">All</option>
<option value="completed">Completed</option>
<option value="uncompleted">Uncompleted</option>
</select>
</div>
</form>
<div class="todo-container">
<ul class="todo-list">
</ul>
</div>
<script src="./app.js"></script>
</body>
</html>