Skip to content

Instantly share code, notes, and snippets.

@allenwyma
Created June 26, 2019 05:31
Show Gist options
  • Save allenwyma/afddf9fa238ddefe6ed17cafce9b222d to your computer and use it in GitHub Desktop.
Save allenwyma/afddf9fa238ddefe6ed17cafce9b222d to your computer and use it in GitHub Desktop.
EventsXtra Test
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