Convert form data to JS object with jQuery

Total Post:341


 1630  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! 
  1. Re: Convert form data to JS object with jQuery

    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[] !== undefined) {
                if (!o[].push) {
                    o[] = [o[]];
                o[].push(this.value || '');
            } else {
                o[] = this.value || '';
        return o;

      Modified On Mar-30-2018 01:02:59 AM


Please check, If you want to make this post sponsored

You are not a Sponsored Member. Click Here to Subscribe the Membership.