Skip to content

Instantly share code, notes, and snippets.

@stricte
Last active August 29, 2015 14:24
Show Gist options
  • Select an option

  • Save stricte/d48e678b39351eb2f942 to your computer and use it in GitHub Desktop.

Select an option

Save stricte/d48e678b39351eb2f942 to your computer and use it in GitHub Desktop.
jQuery(function(){
$.fn.formBackUp = function() {
if(!localStorage) {
return false;
}
var form = this;
var datas = {};
var ls = null;
datas.href = document.location.href;
if(localStorage['formBackUp']) {
ls = JSON.parse(localStorage['formBackUp']);
if(ls.href == datas.href) {
for(var id in ls) {
if(id != 'href') {
$('#'+id).val(ls[id]);
datas[id] = ls.id;
}
}
}
}
form.find('input, textarea').keyup(function(e){
datas[$(this).attr('id')] = $(this).val();
localStorage.setItem('formBackUp', JSON.stringify(datas));
});
form.submit(function(e){
localStorage.removeItem('formBackUp');
});
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment