<input type="date" class="raz" onchange="localStorage.setItem('server', this.value);"> <input type="button" value="очистить localstorage и перезагрузить страницу" onclick="localStorage.clear(); location.reload();" /> <input type="button" value="удалить localstorage определённого элемента и перезагрузить страницу" onclick="localStorage.removeItem('server'); location.reload();" /> <script> var dataRaz = document.querySelector('input[type="date"].raz'); if(localStorage.getItem('server') === null) { // если localStorage.getItem пустой dataRaz.value = new Date().toJSON().slice(0,10); // сегодняшняя дата } else { dataRaz.value = localStorage.getItem('server'); // последняя дата, на которую изменил пользователь } </script> 

How to change this code so that you can enter your text (any) in the text field and that after closing the page, the text entered in the field is preserved?

2 answers 2

  <textarea id="comment"></textarea> <script> var elements = document.querySelectorAll('input, textarea'); function checkValidity() {}; for (i=0; i<elements.length; i++) { (function(element) { var id = element.getAttribute('id'); element.value = localStorage.getItem(id); // обязательно наличие у элементов id element.oninput = function() { localStorage.setItem(id, element.value); checkValidity(); }; })(elements[i]); } </script> 

Here is the answer to the question, but now I don’t know how to sedalat so that the saved text is unique for each page.

     <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="text" class="input"> <script> $('.input').change(function(){ var val_input = $(this).val(); localStorage.setItem('key', val_input); }) var data = localStorage.getItem('key'); //получаем данные </script> 

    • one
      It is better to add a brief description of what happens when this code is executed. - anoam