I am making a to-do list and I am quite stuck on how to implement the "completed task"
So, if I click on a task in a list which is completed, I am addding a class to it which has a text-decoration of line through.
Now, I am stuck on how to update in local storage so that I get those completed lines after closing the browser
function show(){
var todos = get_todos();
var html="<ul>";
for(var i=0; i<todos.length; i++) {
html+= '<li><button class="checked"></button>' + todos[i] + '<button class="remove" id="' + i + '">x</button></li>';
}
html+="</ul>";
$("#todos").html(html);
$(".remove").click(function(event){
event.currentTarget.parentElement.remove();
removeStorage();
});
//tasks which are completed
**$("#todos").on('click','li',function(){**
** $(this).addClass("taskCompleted");**
**});**
}
$("input").keydown(function(e){
if(e.keyCode===13){
event.preventDefault();
add();
}
});
function get_todos(){
var todos = [];
var todos_str = localStorage.getItem('todo');
if(todos_str !=null){
todos = JSON.parse(todos_str);
}
return todos;
}
// Adding a new TODO entry
function add(){
var todos = get_todos();
var task = $('#task').val();
todos.push(task);
localStorage.setItem('todo',JSON.stringify(todos));
//update list of the TODO's displayed on webpage
show()
$("input").val("");
//avoid any further actions generated by 'click' event
return false;
}
//remove the to-do list items from localStorage
function removeStorage(){
var todos = get_todos();
var id = event.currentTarget.getAttribute('id');
todos.splice(id,1);
localStorage.setItem('todo',JSON.stringify(todos));
show();
return false;
}
