Created
June 2, 2019 07:25
-
-
Save smallpaes/582cd7edad5b2d2ea3a352b99aa475cc to your computer and use it in GitHub Desktop.
Update wish list to localStorage
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const wishList = JSON.parse(localStorage.getItem('wishList')) || [] | |
function updateLocalStorage() { | |
//store the list back to localStorage | |
localStorage.setItem('wishList', JSON.stringify(wishList)) | |
} | |
//add event listener to form | |
form.addEventListener('submit', event => { | |
//prevent auto send the form | |
event.preventDefault() | |
//get input value | |
const input = document.querySelector('input[type="text"]') | |
//add new wish to the list | |
displayWish(input.value) | |
//add new wish to the list | |
wishList.push(input.value) | |
//add the wish to localStorage | |
updateLocalStorage(input.value) | |
//clear up the input | |
input.value = '' | |
}) | |
displayArea.addEventListener('click', event => { | |
if (event.target.tagName !== 'SPAN') { return } | |
const li = event.target.parentElement | |
//remove the wish from DOM | |
li.remove() | |
//remove the wish from wishList array | |
wishList.splice(wishList.indexOf(li.textContent.slice(0, -1)), 1) | |
updateLocalStorage() | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment