33

I'm doing the following:

var data = $(form).serializeArray();
// Now I want to  add another value on this data
data.username = 'this is username';

I want to know how can I add another value after doing serializeArray(). I tried all the things I know, but nothing is getting it to work. any ideas pls.

Penny Liu
  • 15,447
  • 5
  • 79
  • 98
Basit
  • 16,316
  • 31
  • 93
  • 154

5 Answers5

59
var data = $(form).serializeArray();
data.push({name: 'username', value: 'this is username'});

see also: jQuery post() with serialize and extra data

Community
  • 1
  • 1
Emmanuel Gleizer
  • 1,990
  • 16
  • 26
41

try

data[data.length] = { name: "username", value: "The Username" };
Lobstrosity
  • 3,928
  • 29
  • 23
  • Is it possible to send an array? I have a property as follows `public HttpPostedFileBase[] Documents { get; set; }` trying to send data in loop – Developer Aug 08 '22 at 16:04
  • Question here https://stackoverflow.com/questions/73194619/how-can-i-add-formdata-with-html-beginform – Developer Aug 08 '22 at 16:05
7

Late to the party, but I personally prefer

const data = $(form).serializeArray().concat({
    name: "username", value: "The Username"
});
Kev
  • 454
  • 6
  • 14
7
var FormAttr = $('#form_id').serializeArray();

FormAttr.push({name: "Name_Of_Attribute", value:"Value_Of_Attributes"});
Harry
  • 87,580
  • 25
  • 202
  • 214
user1210155
  • 103
  • 2
  • 6
-5

I think it's just

data['username'] = 'this is a username';
Josh Pearce
  • 3,399
  • 1
  • 23
  • 24