Skip to content

Instantly share code, notes, and snippets.

@acodesmith
Created March 3, 2015 02:45
Show Gist options
  • Save acodesmith/18731540cd13f1ec83ac to your computer and use it in GitHub Desktop.
Save acodesmith/18731540cd13f1ec83ac to your computer and use it in GitHub Desktop.
HTML Form to Javascript Object
$.fn.formDataObject = function()
{
if(this.is('form')){
var data = {};
for(var i = 0; i < this.serializeArray().length; i++){
var input = this.serializeArray()[i];
if(!$.isEmptyObject(input)){
data[ this.serializeArray()[i].name ] = this.serializeArray()[i].value
}
}
return data;
}
return {};
}
//Usage
$(function(){
//returns a nice formatted object
console.log( $('form').formDataObject() );
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment