Skip to content

Instantly share code, notes, and snippets.

@mattsawyer77
Forked from paulirish/gist:854293
Created November 17, 2011 18:08
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 mattsawyer77/1373955 to your computer and use it in GitHub Desktop.
Save mattsawyer77/1373955 to your computer and use it in GitHub Desktop.
jQuery unserialize Form plugin
// Unserialize (to) form plugin - by Christopher Thielen
// adapted and desuckified (a little) by Paul Irish
// Matt Sawyer changed serialized_values array to an object and handled url-encoding for keys and values
// takes a GET-serialized string, e.g. first=5&second=3&a=b and sets input tags (e.g. input name="first") to their values (e.g. 5)
(function($) {
$.fn.unserializeForm = function(values) {
if (!values) {
return this;
}
values = values.split("&");
var serialized_values = {};
$.each(values, function() {
var properties = this.split("=");
if ((typeof properties[0] != 'undefined') && (typeof properties[1] != 'undefined')) {
serialized_values[unescape(properties[0].replace(/\+/g, " "))] = unescape(properties[1].replace(/\+/g, " "));
}
});
values = serialized_values;
$(this).find(":input").removeAttr('checked').each(function() {
var tag_name = $(this).attr("name");
if (values[tag_name] !== undefined) {
if ($(this).attr("type") == "checkbox") {
$(this).attr("checked", "checked");
} else {
$(this).val(values[tag_name]);
}
}
})
}
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment