How to keep html data saved

Asked

Viewed 144 times

2

Well, I need to take the value of a textarea and send it to a second textarea on the same page, but when I give refresh I don’t want the value of the second textarea to go away, how to do this using javascript and Ajax?.

My Code

var textArea = getDOMElement("textarea", "class", "logarea", 0)
   var y = textArea.value;
   var x = document.createElement("TEXTAREA");
   var t = document.createTextNode(y);
   x.appendChild(t);
   document.getElementsByClassName("span8 center")[0].appendChild(x);

3 answers

1

Cookies

Cookies are data, stored in small text files, on your computer.

When a web server sends a web page to a browser, the connection is terminated and the server forgets all about the user.

Cookies were invented to solve the problem "how to remember user information":

When a user visits a web page, their name may be stored in a cookie. The next time the user visits the page, the cookie "remembers" your name. Cookies are saved in name and value pairs, such as:

document.cookie = "username=John Doe";

1

You can save the value of your textarea in localStorage which is like a database of your nav.

localStorage.setItem('textArea', y);

So whenever you load the page you can fetch the value in localStorage and fill in your field:

x.value = localStorage.getItem('textArea');
  • Well I tried that, but even after cleaning the 1 textarea the second tbm was empty

  • This depends on how you are saving the value of your first textarea, if you save the value of it in localStorage when it is empty, the second tbm will be.

  • I put a button check, it worked obg

1


Use autosave - A jQuery Plugin

<textarea class="form-control" rows="6" id="idTextArea" placeholder="digite aqui ..."></textarea>

<script>  
/*
 jQuery autoSave v1.0.0 - 2013-04-05
 (c) 2013 Yang Zhao - geniuscarrier.com
 license: http://www.opensource.org/licenses/mit-license.php
 */
(function(a){a.fn.autoSave=function(d,e){return this.each(function(){var b=0,c=a(this),f=e||1E3;c.keyup(function(){clearTimeout(b);var a=c.val();localStorage&&localStorage.setItem("autoSave",a);b=setTimeout(function(){d()},f)})})}})(jQuery);

var demoText = $("#idTextArea");


if (localStorage) {  
  var content = localStorage.getItem("autoSave");
  if (content) {
      demoText.text(content);
  }
}

</script>

Source

Browser other questions tagged

You are not signed in. Login or sign up in order to post.