Skip to content

Instantly share code, notes, and snippets.

@goranefbl
Forked from themactep/formtocookie.js
Created April 3, 2017 11:08
Show Gist options
  • Save goranefbl/825a626598a6918aeb64153964396f50 to your computer and use it in GitHub Desktop.
Save goranefbl/825a626598a6918aeb64153964396f50 to your computer and use it in GitHub Desktop.
How to save form to cookies, restore form from cookies. Requires jQuery and jQuery Cookie plugin.
/*
* Save Form To Cookie
* (cc) Paul Philippov, themactep@gmail.com
*
* This is rather a proof of concept than a production-ready solution.
* It does not handle radio buttons and such.
* You might want to extend it to suit your needs.
*
* Usage:
*
* $(function(){
* var myForm = $('#formid');
* loadFormFromCookie(myForm);
* myForm.submit(function() {
* saveFormToCookie(this);
* });
* });
*/
(function ($) {
$.fn.serializeJSON = function () {
var json = {};
jQuery.map($(this).serializeArray(), function (n, _) {
json[n['name']] = n['value'];
});
return json;
};
})(jQuery);
ensureNumber = function (n) {
n = parseInt(n, 10);
if (isNaN(n) || n <= 0) {
n = 0;
}
return n;
};
saveFormToCookie = function (form) {
var name = $(form).attr('id');
var data = JSON.stringify($(form).serializeJSON());
$.cookie(name, data, {expires: 365});
};
loadFormFromCookie = function (form) {
var name = $(form).attr('id');
var data = $.cookie(name);
if (typeof data === 'undefined') {
return;
}
JSON.parse(data, function (key, value) {
if (typeof (value) !== 'object') {
var el = $(form).find('*[name="' + key + '"]');
if (el.is('input')) {
if (false) {
// code formatting stub
} else if (el.attr('type') === 'number') {
el.val(ensureNumber(value));
} else if (el.attr('type') === 'checkbox') {
el.attr('checked', value === 'on' ? true : false);
} else {
el.val(value);
}
} else if (el.is('select')) {
el.val(value);
} else if (el.is('textarea')) {
el.val(value);
}
}
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment