0

When i press save content in textarea need to be saved in localstorage and next time this content in localstorage will be place in textarea when site will be again opened.

Full code in code pen

**JS**
//save txtarea input local storage ..
function SaveBtn(){
var input_textarea = document.querySelector('#result');
var save_button = document.querySelector('#SaveBtn');

save_button.addEventListener('click', updateOutput);

input_textarea.textContent = localStorage.getItem('content');
input_textarea.value = localStorage.getItem('content');

function updateOutput() {
     Del();
 localStorage.setItem('content', input_textarea.value);
    input_textarea.textContent = input_textarea.value;
}
}

Don't have any error or warning when i press save. This is how textarea and save button look like

I want when next time visite site in this text area be 3*3=9 for this user.

  • You don't appear to have a `Del()` function... and you attempt to call it *before* setting the item to `localStorage`. Assuming this in (in theory) hooked up to your delete button, this would remove the content anyway. – Obsidian Age Aug 06 '19 at 21:34
  • This del() is for button fuction (Delete) button who work good. – Aleksa Kovacevic Aug 06 '19 at 21:37
  • 1
    @AleksaKovacevic There is no way to predict how long an item will persist in local storage, I invite you to read this other SO post (https://stackoverflow.com/questions/2326943/when-do-items-in-html5-local-storage-expire). If you really needed to persist a value for a user until their next visit to a website or page you are developing, a database would be the place to put it. – Ryan Wilson Aug 06 '19 at 21:38
  • @RyanWilson Thanks for help – Aleksa Kovacevic Aug 06 '19 at 21:41
  • @AleksaKovacevic Glad to help. – Ryan Wilson Aug 06 '19 at 21:42

1 Answers1

0

  var editor = document.querySelector("#editor");
      if (window.localStorage["TextEditorData"]) {
            editor.value = window.localStorage["TextEditorData"];
      }    
      editor.addEventListener("keyup", function() {
      window.localStorage["TextEditorData"] = editor.value;
      });
<textarea id="editor"></textarea> 


   
Moin Syed
  • 120
  • 7
  • While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value. – β.εηοιτ.βε Aug 05 '20 at 18:03