Skip to content

Instantly share code, notes, and snippets.

@nickstenning
Created April 6, 2009 20:23
Show Gist options
  • Select an option

  • Save nickstenning/90917 to your computer and use it in GitHub Desktop.

Select an option

Save nickstenning/90917 to your computer and use it in GitHub Desktop.
Element.implement({
toObject: function () {
var obj = {};
this.getElements('input, select, textarea, button').each(function(el){
if (!el.name || el.disabled) return;
var value = (el.tagName.toLowerCase() == 'select') ? Element.getSelected(el).map(function(opt){
return opt.value;
}) : ((el.type == 'radio' || el.type == 'checkbox') && !el.checked) ? null : el.value;
$splat(value).each(function(val){
if (typeof val !== "undefined") { obj[el.name] = val; }
});
});
return obj;
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment