This function will take your entire HTML form and convert all the inputs into a single JS object! Thanks to Dave over at Stack Overflow!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
/** * Take a form and convert it into a Object for easy manipulation * * @link https://stackoverflow.com/a/23315254 * @link http://jsfiddle.net/akhildave/sxGtM/5002/ */ $.fn.serializeObject = function() { var o = {}; var a = this.serializeArray(); $.each(a, function() { if (o[this.name] !== undefined) { if (!o[this.name].push) { o[this.name] = [o[this.name]]; } o[this.name].push(this.value || ''); } else { o[this.name] = this.value || ''; } }); return o; }; |
Usage is as simple as: