jQuery serializeArray() add another value on top for passing to Ajax

46.3k views Asked by At

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.

5

There are 5 answers

2
Lobstrosity On BEST ANSWER

try

data[data.length] = { name: "username", value: "The Username" };
0
Josh Pearce On

I think it's just

data['username'] = 'this is a username';
0
Kev On

Late to the party, but I personally prefer

const data = $(form).serializeArray().concat({
    name: "username", value: "The Username"
});
0
Emmanuel Gleizer On
var data = $(form).serializeArray();
data.push({name: 'username', value: 'this is username'});

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

0
user1210155 On
var FormAttr = $('#form_id').serializeArray();

FormAttr.push({name: "Name_Of_Attribute", value:"Value_Of_Attributes"});