1

I should think my question is radically different from

jQuery post() with serialize and extra data

for it does not relate to adding data to the array output but to extending the contents of the array objects.

jQuery serializeArray produces an array like so:

[ Object { name="myElement",  value="any value"}, etc ]

Is there a way to extend the content of the object so that it includes for example:

[ Object { name="myElement",  value="any value", class="any classes"}, etc ]

This of course could save you a lot of time rather than 'manually' prepare an object by traversing all input elements and pass it to an XHR function.

Brice Coustillas
  • 2,363
  • 2
  • 13
  • 18

1 Answers1

1

Try this

var arr = $('form').serializeArray();
var newArr = [];
$(arr).each(function(index, item){  
    var className = $('[name="'+item.name+'"]').attr('class');
    item["class"] = className ? className : "";
    newArr.push(item);
})
console.log(newArr);

Here newArr will have class also.

Manoj
  • 4,951
  • 2
  • 30
  • 56