32

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
  • 11,885
  • 5
  • 66
  • 81
Basit
  • 15,058
  • 31
  • 91
  • 148

5 Answers5

57
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,917
  • 16
  • 26
40

try

data[data.length] = { name: "username", value: "The Username" };
Lobstrosity
  • 3,888
  • 28
  • 23
6
var FormAttr = $('#form_id').serializeArray();

FormAttr.push({name: "Name_Of_Attribute", value:"Value_Of_Attributes"});
Harry
  • 83,910
  • 24
  • 185
  • 203
user1210155
  • 93
  • 2
  • 6
5

Late to the party, but I personally prefer

const data = $(form).serializeArray().concat({
    name: "username", value: "The Username"
});
Kev
  • 414
  • 5
  • 14
-5

I think it's just

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