Last active
August 29, 2015 13:58
-
-
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)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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