Skip to content

Instantly share code, notes, and snippets.

@Kcko
Forked from d3nj3ll/autosave.js
Created July 12, 2017 17:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Kcko/b31e3e54d9e5531d266c46f0875f6b7a to your computer and use it in GitHub Desktop.
Save Kcko/b31e3e54d9e5531d266c46f0875f6b7a to your computer and use it in GitHub Desktop.
Client-side Autosave Using localStorage with jQuery in textarea
$(document).ready(function() {
///////////////////////
//
// Recovery Below
//
///////////////////////
// Retrieve the object from storage onReady
var autosave = localStorage.getItem('file');
// parses the string (btw. its UTF-8)
var text = JSON.parse(autosave);
//modifies the textarea with the id="inputTextArea"
$("textarea#inputTextArea").val(text);
////////////////////////
//
// Autosaver below
//
////////////////////////
// Autosave on keystroke works in offline mode
$("textarea#inputTextArea").change (function(){
// pulls the value from the textarea
var file = $('textarea#inputTextArea').val();
// sets the file string to hold the data
localStorage.setItem('file', JSON.stringify(file));
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment