user1150087
user1150087

Reputation:

Serialize and jquery object

I tried to use a jquery object to serialize some of values in a form. I tried using a div id that surrounded the fields I needed like this:

var test = $("#div_tab1").serialize() and it was empty

var test = $("#form1").serialize() worked. 

shouldn't the first example work?

I am using version 1.6.4

Upvotes: 2

Views: 449

Answers (5)

Max Stern
Max Stern

Reputation: 180

In case you do not want to serialize the whole form, you can serialize elements within a specific #id or even a specific element name.

// serialize input elements within a specific #id
$('#id :input').serialize();


// serialize a specific element in the form
$('input[name=inputName]').serialize();

Upvotes: 0

Sameera Thilakasiri
Sameera Thilakasiri

Reputation: 9508

I’ve found that serialize works on a form or a jQuery of :input tags, but not a div that contains :input tags.

Upvotes: 0

Blender
Blender

Reputation: 298176

From the API:

.serialize():

Description: Encode a set of form elements as a string for submission.

Notice how it says form elements

.serialize() returns a string that is meant to be placed in URLs: ?bar=baz&foo2=bar.

It won't make sense for a non-form element, as it doesn't have a value attribute.

Upvotes: 0

ShankarSangoli
ShankarSangoli

Reputation: 69905

serialize works only on a form or form elements.

Upvotes: 0

user1106925
user1106925

Reputation:

"shouldn't the first example work?"

No, you need to call it on a form, or on a set of input elements.

Upvotes: 2

Related Questions