HiveBrain v1.2.0
Get Started
← Back to all entries
snippetjavascriptCritical

Convert form data to JavaScript object with jQuery

Submitted by: @import:stackoverflow-api··
0
Viewed 0 times
formdataobjectwithjqueryconvertjavascript

Problem

How do I convert all elements of my form to a JavaScript object?

I'd like to have some way of automatically building a JavaScript object from my form, without having to loop over each element. I do not want a string, as returned by $('#formid').serialize();, nor do I want the map returned by $('#formid').serializeArray();

Solution

serializeArray already does exactly that. You just need to massage the data into your required format:
`function objectifyForm(formArray) {
//serialize data function
var returnArray = {};
for (var i = 0; i

Watch out for hidden fields which have the same name as real inputs as they will get overwritten.

Context

Stack Overflow Q#1184624, score: 1741

Revisions (0)

No revisions yet.