Skip to content

Commit b9167fe

Browse files
added a seperate bar to look all the completed and uncompleted one and added some css to it . Even added to store in local storage
1 parent 19e9262 commit b9167fe

File tree

3 files changed

+143
-1
lines changed

3 files changed

+143
-1
lines changed

app.js

+93-1
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@
22
const todoInput = document.querySelector('.todo-input');
33
const todoButton = document.querySelector('.todo-button');
44
const todoList = document.querySelector('.todo-list');
5+
const filterOption = document.querySelector('.filter-todo');
56

67
// Event Listeners
8+
document.addEventListener('DOMContentLoaded', getTodos);
79
todoButton.addEventListener('click', addTodo);
810
todoList.addEventListener('click', deleteCheck);
11+
filterOption.addEventListener('change', filterTodo); // Changed from 'click' to 'change'
912

1013
// Functions
1114
function addTodo(event) {
@@ -21,6 +24,9 @@ function addTodo(event) {
2124
newToDo.innerText = todoInput.value;
2225
todoDiv.appendChild(newToDo);
2326

27+
//add todo to local storage
28+
saveLocalTodos(todoInput.value);
29+
2430
// Check Mark button
2531
const completedButton = document.createElement('button');
2632
completedButton.innerHTML = '<i class="fas fa-check"></i>';
@@ -49,7 +55,7 @@ function deleteCheck(e){
4955

5056
//fallout animation
5157
todo.classList.add('fall');
52-
58+
removeLocalTodos(todo);
5359
//wait for a brief moment before removing the todo
5460
setTimeout(function() {
5561
todo.remove();
@@ -62,3 +68,89 @@ function deleteCheck(e){
6268
todo.classList.toggle('completed');
6369
}
6470
}
71+
72+
function filterTodo(e){
73+
const todos = todoList.childNodes;
74+
todos.forEach(function(todo){
75+
switch(e.target.value){
76+
case "all":
77+
todo.style.display='flex';
78+
break;
79+
case "completed":
80+
if(todo.classList.contains('completed')){
81+
todo.style.display='flex';
82+
}else{
83+
todo.style.display='none';
84+
}
85+
break;
86+
case "uncompleted":
87+
if(!todo.classList.contains('completed')){
88+
todo.style.display='flex';
89+
}else{
90+
todo.style.display='none';
91+
}
92+
break;
93+
}
94+
});
95+
}
96+
97+
function saveLocalTodos(todo){
98+
let todos;
99+
if(localStorage.getItem('todos')===null){
100+
todos=[];
101+
}
102+
else{
103+
todos = JSON.parse(localStorage.getItem('todos')); // assign the parsed data back to todos
104+
}
105+
todos.push(todo);
106+
localStorage.setItem('todos', JSON.stringify(todos));
107+
}
108+
109+
function getTodos(){
110+
let todos;
111+
if(localStorage.getItem('todos')===null){
112+
todos=[];
113+
}
114+
else{
115+
todos=JSON.parse(localStorage.getItem('todos'));
116+
}
117+
todos.forEach(function(todoItem){ // rename the parameter to todoItem
118+
// Create a div in HTML
119+
const todoDiv = document.createElement('div');
120+
todoDiv.classList.add('todo');
121+
122+
// Now adding LI
123+
const newToDo = document.createElement('li');
124+
newToDo.classList.add('todo-item');
125+
newToDo.innerText = todoItem; // use todoItem here
126+
todoDiv.appendChild(newToDo);
127+
128+
// Check Mark button
129+
const completedButton = document.createElement('button');
130+
completedButton.innerHTML = '<i class="fas fa-check"></i>';
131+
completedButton.classList.add('complete-btn');
132+
todoDiv.appendChild(completedButton);
133+
134+
// Trash button
135+
const trashButton = document.createElement('button');
136+
trashButton.innerHTML = '<i class="fas fa-trash"></i>';
137+
trashButton.classList.add('trash-btn');
138+
todoDiv.appendChild(trashButton);
139+
140+
// Append to List
141+
todoList.appendChild(todoDiv);
142+
});
143+
}
144+
145+
function removeLocalTodos(todo){
146+
let todos;
147+
if(localStorage.getItem('todos')===null){
148+
todos=[];
149+
}
150+
else{
151+
todos=JSON.parse(localStorage.getItem('todos'));
152+
}
153+
const todoIndex=todo.children[0].innerText;
154+
todos.splice(todos.indexOf(todoIndex),1);
155+
localStorage.setItem('todos', JSON.stringify(todos));
156+
}

index.html

+7
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,13 @@ <h1>Your Todo-List</h1>
1919
<button class="todo-button" type="submit">
2020
<i class="fas fa-plus-square"></i>
2121
</button>
22+
<div class="select">
23+
<select name="todos" class="filter-todo">
24+
<option value="all">All</option>
25+
<option value="completed">Completed</option>
26+
<option value="uncompleted">Uncompleted</option>
27+
</select>
28+
</div>
2229
</form>
2330
<div class="todo-container">
2431
<ul class="todo-list"></ul>

style.css

+43
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
2+
13
*{
24
margin: 0;
35
padding: 0;
@@ -106,3 +108,44 @@ form button:hover {
106108
opacity: 0;
107109
transition: transform 0.7s ease, opacity 0.7s ease;
108110
}
111+
112+
select{
113+
-webkit-appearance: none;
114+
-moz-appearance: none;
115+
appearance: none;
116+
outline: none;
117+
border: none;
118+
transition: background-color 0.10s ease;
119+
}
120+
121+
.select{
122+
margin: 1rem;
123+
position: relative;
124+
overflow: hidden;
125+
}
126+
127+
select{
128+
color:black;
129+
width: 10rem;
130+
cursor: pointer;
131+
padding: 1rem;
132+
}
133+
134+
.select::after{
135+
content: "\25BC";
136+
position: absolute;
137+
top: 0;
138+
right: 0;
139+
padding: 1rem;
140+
pointer-events: none;
141+
transition: background-color 0.7s ease;
142+
}
143+
144+
.select:hover::after{
145+
color: white;
146+
background-color: black;
147+
}
148+
select:hover{
149+
background-color: black;
150+
color:white
151+
}

0 commit comments

Comments
 (0)