Skip to content

Instantly share code, notes, and snippets.

@kangax
Created October 14, 2008 20:01
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 kangax/16780 to your computer and use it in GitHub Desktop.
Save kangax/16780 to your computer and use it in GitHub Desktop.
var serializeElements = (function(){
function isSuccessful(element) {
// http://www.w3.org/TR/html401/interact/forms.html#h-17.13.2
return (
!element.disabled && // should not be disabled
element.name && // should have a name
!'^file|reset$'.test(element.type) && // should not be of `file` or `reset` types
Element.getValue(element) != null // should have a non-null value
);
}
return function(elements, options) {
if (typeof options != 'object') {
options = { hash: !!options };
}
else if (Object.isUndefined(options.hash)) {
options.hash = true;
}
var submitted = false,
submit = options.submit;
var data = elements.inject([], function(data, element) {
if (isSuccessful(element) && (element.type != 'submit' || (!submitted &&
// TODO: this is confusing
submit !== false && (!submit || element.name == submit) && (submitted = true)))) {
var temp = {};
o[element.name] = Element.getValue(element);
data.push(o);
}
return data;
});
return options.hash ? data : Object.toQueryString(data);
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment