.serializeArray()
S2C Home « Ajax « .serializeArray()
Form element array encryption.
Description
The .serializeArray()
Ajax method, allows us to encode a set of form elements as an array of name/value pairs ready for encoding as a JSON string.
- The
.serializeArray()
method returns an array of objects ready to be encoded as a JSON string and operates on a jQuery object representing a set of form elements. - The
.serializeArray()
method can act on a jQuery object that has form elements of several types if required, although it is generally easier to select the <form> element. - Only 'successful controls' are serialized to the returned string as outlined in the
W3C Proposals
.
Syntax
Signature | Description |
---|---|
.serializeArray() | Encode a set of form elements as an array of name/value pairs ready for encoding as a JSON string. |
Parameters
None.
Return
An Array
object.
.serializeArray()
ExampleTop
Encode a set of form elements as an array of name/value pairs ready for encoding as a JSON string.
In the example below when we press the 'Submit' button we serialize the 'successful controls' from the form below into an array of name/value pairs. For the example we output a message but the resultant array could just as easily be encoded into a JSON string. Change some values in the form and press the 'Submit' button to see the varying results.
$(function(){
$('#form2').submit(function() {
$('#div2').append(JSON.stringify($(this).serializeArray()) + '<br>');
return false;
});
;});
div2. Some initial text.
Related Tutorials
jQuery Advanced Tutorials - Lesson 10 - Ajax Helper Functions