Created
June 26, 2019 05:31
-
-
Save allenwyma/afddf9fa238ddefe6ed17cafce9b222d to your computer and use it in GitHub Desktop.
EventsXtra Test
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 MEMO_STORAGE_KEY = 'memoItems'; | |
const uiDivId = 'memo-items-list'; | |
const uiDiv = document.getElementById(uiDivId); | |
const textbox = document.getElementById('memo-textbox'); | |
const appendMemo = (text) => { | |
const memoItem = document.createElement('p'); | |
const number = document.querySelectorAll(`#${uiDivId} p`).length + 1; | |
memoItem.innerHTML = `${number}. ${text}`; | |
uiDiv.appendChild(memoItem); | |
} | |
textbox.addEventListener('keypress', (e) => { | |
const value = textbox.value.trim(); | |
if(e.keyCode === 13 && value.length > 0) { // enter keyCode | |
// grab localStorage | |
const memoItems = JSON.parse(localStorage.getItem(MEMO_STORAGE_KEY) || '[]'); | |
// push value | |
memoItems.push(value); | |
// save to localStorage | |
localStorage.setItem(MEMO_STORAGE_KEY, JSON.stringify(memoItems)); | |
// update UI | |
appendMemo(value); | |
// reset textbox | |
textbox.value = ''; | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment