Home > DeveloperSection > Forums > Convert form data to JS object with jQuery
Ankit Singh

Total Post:341

Posted on    May-29-2013 1:18 AM


 1 Reply(s)
 1024  View(s)
Rate this:
Hi Developers,

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

I'd like to have some way of automatically building a JS 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();

Thanks in advance! 


Total Post:604

Posted on    May-29-2013 8:53 AM

Hi Ankita,

serializeArray already does exactly that, you just need to massage the data into your required format:

Try as following

$.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;

Don't want to miss updates? Please click the below button!

Follow MindStick