Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //If input value is empty alert "Enter all fields" return false
- //open localStorage using localStorage.getItem("name of your to-do"). Here I put it in a variable called record.
- //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.
- // Push the created object (recordObj) to the created list called todoList.
- //Stringify the list and set it to localStorage using localStorage.setItem.
- //else if the localStorage exists, get localStorage using localStorage.get.
- // Convert it to json using json.parse (I put it in a variable called result)
- //Create a new object and add the content in the input ( The input tag in the frontend/UI).
- //Push it to the result.
- //stringify the result
- // set it back in the localStorage using localStorage.setItem.
- //NB td and tn are Todo name and Todo Description.
- //NB tn.value is the text from the text input for Todo name in the UI and td same for Todo Description.
- // Ask any question on WhatsApp.
- function addTodoValue(){
- if (!tn.value || !td.value){
- alert('Enter required fields');
- return false;
- }
- let record = localStorage.getItem('todo');
- if (!record){
- let todoList = [];
- recordObj = {"todoName":tn.value,
- "todoDesc":td.value};
- todoList.push(recordObj)
- localStorage.setItem('todo', JSON.stringify(todoList));
- }
- else{
- recordObj = {"todoName":tn.value,
- "todoDesc":td.value};
- result = localStorage.getItem('todo');
- result = JSON.parse(result)
- result.push(recordObj);
- localStorage.setItem('todo', JSON.stringify(result));
- }
- tn.value = '';
- td.value = '';
- }// End of add function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement