class method Form.serializeElements

View source on GitHub →

Form.serializeElements(elements[, options]) → String | Object
  • elements (Array) – A collection of elements to include in the serialization.
  • options (Object)(Optional) A set of options that affect the return value of the method.

Serialize an array of form elements to an object or string suitable for Ajax requests.

As per the HTML spec, disabled fields are not included.

If multiple elements have the same name and we're returning an object, the value for that key in the object will be an array of the field values in the order they appeared on the array of elements.

The preferred method to serialize a form is Form.serialize. Refer to it for further information and examples on the hash option. However, with Form.serializeElements you can serialize specific input elements of your choice, allowing you to specify a subset of form elements that you want to serialize data from.

The Options

The options allow you to control two things: What kind of return value you get (an object or a string), and whether and which submit fields are included in that object or string.

If you do not supply an options object at all, the options { hash: false } are used.

If you supply an options object, it may have the following options:

  • hash (Boolean): true to return a plain object with keys and values (not a Hash; see below), false to return a String in query string format. If you supply an options object with no hash member, hash defaults to true. Note that this is not the same as leaving off the options object entirely (see above).

  • submit (Boolean | String): In essence: If you omit this option the first submit button in the form is included; if you supply false, no submit buttons are included; if you supply the name of a submit button, the first button with that name is included. Note that the false value must really be false, not falsey; falsey-but-not-false is like omitting the option.

(Deprecated) If you pass in a Boolean instead of an object for options, it is used as the hash option and all other options are defaulted.

A hash, not a Hash

If you opt to receive an object, it is a plain JavaScript object with keys and values, not a Hash. All JavaScript objects are hashes in the lower-case sense of the word, which is why the option has that somewhat-confusing name.

Examples

To serialize all input elements of type "text":

Form.serializeElements( $('myform').getInputs('text') )
// -> serialized data