Skip to content

Instantly share code, notes, and snippets.

@jtdevos
Last active August 29, 2015 13:58
Show Gist options
  • Save jtdevos/9945563 to your computer and use it in GitHub Desktop.
Save jtdevos/9945563 to your computer and use it in GitHub Desktop.
example of how to serialize all of the checked boxes on a form, and how to restore that state later on (requires jquery)
//return serialized list of checked checkboxes
function cb2str() {
return JSON.stringify($.map($("[id]:checkbox:checked").get(), function(el){return el.id}))
}
//check the boxes from a serialized list of checkboxes
function str2cb(str) {
$.each(JSON.parse(str), function(i, cbid){
$("#" + cbid).prop('checked', true)
});
}
//Example 1: serialize to a popup (alternately you could save to a global or to localstorage)
alert(cb2str());
//Example 2: load serialized data from a popup
str2cb(prompt("enter serialized string"));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment