javascript - Local storage remove.item -


i have todo app.i made jquery , localstorage.codes correct ı can't delete tasks localstorage

function appendtasktolist(val) {     $('#list').append($("<li id='task-' + >  <a href='#' class='done-btn'>done</a>" +" " + val + " <a href='javascript:void(0)' class='cancel-btn'>delete</a></li>").data("erdem", "12")); }  if (localstorage['tasks']) {     var tasks = json.parse(localstorage['tasks']); }else {     var tasks = []; }  for(var i=0;i<tasks.length;i++) {     appendtasktolist(tasks[i]);  }  var addtask = function(){     var val = $('#name').val();     tasks.push(val);     localstorage["tasks"] = json.stringify(tasks);     appendtasktolist(val);     $('#name').val("").focus(); };  localstorage.username = $('#add-btn').click(addtask); $('#name').keyup(function(e){     if (e.keycode === 13) {         addtask();     } });  $(document).delegate('.done-btn', 'click', function() {     $(this).parent('li').addclass('done');     return false; });  $(document).delegate('.cancel-btn', 'click', function() {     $(this).parent('li').remove();     localstorage.removeitem($(this).parent('li').remove()); }); 

please me , sorry bad english

the removeitem() method of local storage, when passed key name, remove key storage.

for code can extract name of task li , use update tasks array , update local storage tasks value.

for code:

//extract task name li element var nameoftask = $(this).parent('li').clone().children().remove().end().text().trim(); //find index in task element var index = tasks.indexof(nameoftask);  tasks.splice(index,1); localstorage["tasks"] = json.stringify(tasks); 

please see ref: 'https://jsfiddle.net/bhtvt1c3/1/'


Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -