Created
March 3, 2015 02:45
-
-
Save acodesmith/18731540cd13f1ec83ac to your computer and use it in GitHub Desktop.
HTML Form to Javascript Object
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$.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