Advertisement
collinsanele

addTodoValue function explained

Sep 13th, 2019
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //If input value is empty alert "Enter all fields" return false
  2.  
  3. //open localStorage using localStorage.getItem("name of your to-do"). Here I put it in a variable called record.
  4.  
  5. //if not record, that is if the localStorage (record) doesn't exist. Create a new array I called mine todoList, create a new object I called mine recordObj.
  6. // Push the created object (recordObj) to the created list called todoList.
  7.  
  8. //Stringify the list and set it to localStorage using localStorage.setItem.
  9.  
  10. //else if the localStorage exists, get localStorage using localStorage.get.
  11.  
  12. // Convert it to json using json.parse (I put it in a variable called result)
  13.  
  14. //Create a new object and add the content in the input ( The input tag in the frontend/UI).
  15.  
  16. //Push it to the result.
  17.  
  18. //stringify the result
  19.  
  20. // set it back in the localStorage using localStorage.setItem.
  21.  
  22. //NB td and tn are Todo name and Todo Description.
  23.  
  24. //NB tn.value is the text from the text input for Todo name in the UI and td same for Todo Description.
  25.  
  26. // Ask any question on WhatsApp.
  27.  
  28.  
  29.  
  30.  
  31.  
  32.  
  33.  
  34. function addTodoValue(){
  35.        if (!tn.value || !td.value){
  36.          alert('Enter required fields');
  37.          return false;
  38.        }
  39.        let record = localStorage.getItem('todo');
  40.        if (!record){
  41.          let todoList = [];
  42.          recordObj = {"todoName":tn.value,
  43.                       "todoDesc":td.value};
  44.          todoList.push(recordObj)
  45.          localStorage.setItem('todo', JSON.stringify(todoList));
  46.        }
  47.        else{
  48.      
  49.          recordObj = {"todoName":tn.value,
  50.                        "todoDesc":td.value};
  51.          
  52.          result = localStorage.getItem('todo');
  53.          result = JSON.parse(result)
  54.          result.push(recordObj);
  55.          localStorage.setItem('todo', JSON.stringify(result));
  56.        }
  57.        tn.value = '';
  58.        td.value = '';
  59.     }// End of add function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement