Auto Save Textarea to localStorage
JavaScript
Medium
7 views
Problem Description
You have and a . Save textarea value to localStorage key note_text on input (debounced 200ms). On load, restore. Clear button removes storage and clears textarea.
Output Format
No output (DOM change).
Constraints
Assume #note textarea and #clear button exist.
Official Solution
const note=document.getElementById('note');const clear=document.getElementById('clear');const key='note_text';note.value=localStorage.getItem(key)||'';let t=0;note.addEventListener('input',()=>{clearTimeout(t);t=setTimeout(()=>{localStorage.setItem(key,note.value);},200);});clear.addEventListener('click',()=>{localStorage.removeItem(key);note.value='';note.focus();});
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!