Skip to content

Instantly share code, notes, and snippets.

@ralt
Last active December 17, 2015 07:39
Show Gist options
  • Save ralt/5574170 to your computer and use it in GitHub Desktop.
Save ralt/5574170 to your computer and use it in GitHub Desktop.
function serialize(form) {
return [].reduce.call(form.elements, function(r, element) {
return r + (r === '' ? '' : '&') + (function() {
switch (element.type) {
case 'checkbox':
return name(element.checked);
case 'radio':
return element.checked ? name(element.value) : '';
default:
return name(element.value);
}
function name(v) {
return element.name + '=' + encodeURIComponent(v);
}
}());
}, '');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment