Basit
Basit

Reputation: 17214

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

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.

Upvotes: 34

Views: 47254

Answers (5)

Kev
Kev

Reputation: 454

Late to the party, but I personally prefer

const data = $(form).serializeArray().concat({
    name: "username", value: "The Username"
});

Upvotes: 7

user1210155
user1210155

Reputation: 103

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

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

Upvotes: 7

Emmanuel Gleizer
Emmanuel Gleizer

Reputation: 2018

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

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

Upvotes: 60

Lobstrosity
Lobstrosity

Reputation: 3936

try

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

Upvotes: 42

Josh Pearce
Josh Pearce

Reputation: 3455

I think it's just

data['username'] = 'this is a username';

Upvotes: -5

Related Questions