Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

how to prevent todos from vanishing after refreshing page - javascript

const addButton = document.querySelector("#addToDo");
const delBtn = document.querySelector("#delToDo");

addButton.addEventListener("click", addTask, false);
var tasksID = 0;

function saveTask(taskID, taskInfo) {
  // Get tasks from local storage
  let tasks = localStorage.getItem("todo-tasks");

  if (!tasks) {
    tasks = {};
  } else {
    tasks = JSON.parse(tasks);
    tasks[taskID] = taskInfo;
  }

  // Save the result back
  let infobj = JSON.stringify(tasks);
  localStorage.setItem("todo-tasks", infobj);
}

function drawSavedTasks() {
  let tasks = localStorage.getItem("todo-tasks");
  if (tasks) {
    tasks = JSON.parse(tasks);
    Object.keys(tasks).forEach(k => {
      addTask(null, tasks[k]);
    });
  }
}

drawSavedTasks();

function addTask(e, textToDo = undefined) {
  if (!textToDo) {
    textToDo = document.querySelector("#toDo").value;
  }

  var list = document.querySelector(".list");
  var divToDo = document.createElement("div");
  var p = document.createElement("p");
  var delButton = document.createElement("button");

  divToDo.setAttribute("id", "todo" + tasksID);
  divToDo.setAttribute("class", "toDo");
  delButton.classList.add("delToDo");

  delButton.textContent = "Delete";
  p.textContent = textToDo;

  delButton.onclick = function() {
    divToDo.parentNode.removeChild(divToDo);
  }

  divToDo.appendChild(p);
  divToDo.appendChild(delButton);

  list.appendChild(divToDo);
  saveTask(tasksID, textToDo);
  ++tasksID;
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: highcharts react hide data point dots 
Javascript :: this 
Javascript :: CalendarApp.newRecurrence 
Javascript :: firebase ststokenmanager return undefined 
Javascript :: create 24 hours array like 00:00 to 23:30 
Javascript :: Functions & Exec Context makePlans 
Javascript :: for loop shothand in js 
Javascript :: javascript covert html characters to text 
Javascript :: react Mixed symbols 
Javascript :: react-select is unable to remove selected option after adding value props 
Javascript :: settimerout get throw out after refresh browser 
Javascript :: return multiple native element react native 
Javascript :: js find :invalid inside div 
Javascript :: discord javascript error on startup 
Javascript :: how to get file tree of specific folder in node js 
Javascript :: Spread Syntax for function 
Javascript :: form validation jflutter 
Javascript :: regular expressiong to indentify bible references in a sentence 
Javascript :: form react js 
Javascript :: a complex label expression before a colon must be parenthesized 
Javascript :: Uncaught SyntaxError: Unexpected end of JSON input at JSON.parse 
Javascript :: javascript promises mdn 
Javascript :: types of directive in jsp 
Javascript :: for await range javascript 
Javascript :: search for country name from api with react 
Javascript :: window coordinates 
Javascript :: test command in node js 
Javascript :: can not find static files on multilevel routes in express js 
Javascript :: mongoose undo delete 
Javascript :: javascript set default button form 
ADD CONTENT
Topic
Content
Source link
Name
5+4 =