jQuery API

.serializeArray()

.serializeArray() Returns: Array

Description: Encode a set of form elements as an array of names and values.

  • version added: 1.2.serializeArray()

The .serializeArray() method creates a JavaScript array of objects, ready to be encoded as a JSON string. It operates on a jQuery object representing a set of form elements. The form elements can be of several types:

<form>
  <div><input type="text" name="a" value="1" id="a" /></div>
  <div><input type="text" name="b" value="2" id="b" /></div>
  <div><input type="hidden" name="c" value="3" id="c" /></div>
  <div>
    <textarea name="d" rows="8" cols="40">4</textarea>
  </div>
  <div><select name="e">
    <option value="5" selected="selected">5</option>
    <option value="6">6</option>
    <option value="7">7</option>
  </select></div>
  <div>
    <input type="checkbox" name="f" value="8" id="f" />
  </div>
  <div>
    <input type="submit" name="g" value="Submit" id="g" />
  </div>
</form>

The .serializeArray() method uses the standard W3C rules for successful controls to determine which elements it should include; in particular the element cannot be disabled and must contain a name attribute. No submit button value is serialized since the form was not submitted using a button. Data from file select elements is not serialized.

This method can act on a jQuery object that has selected individual form elements, such as <input>, <textarea>, and <select>. However, it is typically easier to select the <form> tag itself for serialization:

$('form').submit(function() {
  console.log($(this).serializeArray());
  return false;
});

This produces the following data structure (provided that the browser supports console.log):

[
  {
    name: "a",
    value: "1"
  },
  {
    name: "b",
    value: "2"
  },
  {
    name: "c",
    value: "3"
  },
  {
    name: "d",
    value: "4"
  },
  {
    name: "e",
    value: "5"
  }
]

Example:

Get the values from a form, iterate through them, and append them to a results display.

<!DOCTYPE html>
<html>
<head>
  <style>
  body, select { font-size:14px; }
  form { margin:5px; }
  p { color:red; margin:5px; }
  b { color:blue; }
  </style>
  <script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
  <p><b>Results:</b> <span id="results"></span></p>

  <form>
    <select name="single">
      <option>Single</option>
      <option>Single2</option>

    </select>
    <select name="multiple" multiple="multiple">
      <option selected="selected">Multiple</option>
      <option>Multiple2</option>

      <option selected="selected">Multiple3</option>
    </select><br/>
    <input type="checkbox" name="check" value="check1" id="ch1"/>

    <label for="ch1">check1</label>
    <input type="checkbox" name="check" value="check2" checked="checked" id="ch2"/>

    <label for="ch2">check2</label>
    <input type="radio" name="radio" value="radio1" checked="checked" id="r1"/>

    <label for="r1">radio1</label>
    <input type="radio" name="radio" value="radio2" id="r2"/>

    <label for="r2">radio2</label>
  </form>
<script>

    function showValues() {
      var fields = $(":input").serializeArray();
      $("#results").empty();
      jQuery.each(fields, function(i, field){
        $("#results").append(field.value + " ");
      });
    }

    $(":checkbox, :radio").click(showValues);
    $("select").change(showValues);
    showValues();
</script>

</body>
</html>

Demo:

Support and Contributions

Need help with .serializeArray() or have a question about it? Visit the jQuery Forum or the #jquery channel on irc.freenode.net.

Think you've discovered a jQuery bug related to .serializeArray()? Report it to the jQuery core team.

Found a problem with this documentation? Report it on the GitHub issue tracker

  • http://www.viazanie.eu/ Viazanie diplomoviek

    Is here a function to get array (key=>val) of inputs/selects out of form?

    • Eduardo García Sanz

      I really think we should have an option to get an array like

      [{element_name: element_value}...] because is lighter and easiest to convert to something like $formData['element_name'] (I’m using PHP but you already got the point right?).

    • http://twitter.com/jhcaiced Jhon H. Caicedo

      Hi, I just wrote a small code to do this conversion in case someone find it useful.

      var params = {};
      jQuery.each(jQuery('#myForm').serializeArray(), function(index,value) {
      params[value.name] = value.value;
      });

      • http://kflorence.myopenid.com/ Kyle Florence

        That won't work well for multiple checkboxes as they all have the same name, so the value will just get overwritten. Instead, it should check to see if value.name already exists inside of params, and if it does turn it into an array and push in the new value (or turn it into an object and append the new property using the checkbox's index as the key).

  • Anonymous

    serializeArray() doesn’t serialize field like input type=’file’, is it normal ??

    • Anonymous

      I think if you check the documentation I seem to recall that it doesn’t serialize type ‘file’. So yes this is normal

  • Anonymous

    I am getting nothing back except [Object object], [Object object], etc.

    Why would this happen?

    • http://profiles.yahoo.com/u/5OBUDUMQIQTWIZAHMQA5HNUCHA Alexandre

      That’s because the returned array elements contain objects with properties “name” and “value”. You can access them list that:

      arSerialized[0].name
      arSerialized[0].value

  • Phil

    You can easily build a JSON object out of the array using just core javascript. Here’s how I did it:

    var query = searchForm.serializeArray(),
    json = {};

    for (i in query) {
    json[query[i].name] = query[i].value
    }

  • Anonymous

    $.extend($, {
    requestObject: function(obj, prefix) {
    if (typeof obj == “object”) {

    if (!prefix || prefix == “”) prefix = “”;
    else { prefix += (prefix.charAt(prefix.length – 1) == “.”) ? “” : “.”; }

    var result = {};
    for (var key in obj) {
    var recurObjs = $.requestObject(obj[key], “.”);
    var propName = (isNaN(key)) ? prefix + key : “[" + key + "]“;

    if (typeof recurObjs == “object”)
    $.each(recurObjs, function(recurKey, recurObj) { result[propName + recurKey] = recurObj; });
    else
    result[propName] = recurObjs;
    }

    return result;
    }

    return obj;
    }
    });

    i write $.requestObject() to convert from javascript object to request object.
    example:
    var exampleObj = { Names: [{ Name: "A" }, { Name: "B"}], Position: “Programmer” };
    $.getJSON(“/getjson”,$.requestObject(exampleObj),function(data){});
    //this result look like : { Names[0].Name : “A”, Names[1].Name : “B”, Position : “Programmer” }
    Try this function, it might help you.

    contact me @cs_jo

  • Dan S

    Note: This function will not work unless the selected s have the ‘name’ attribute defined.

  • creshkendz

    Hi,

    Thank You. However, I have a problem, how can I filter those inputs to be serialized like I don’t want to include those type=hidden in my serializeArray()?

    Can you please help me?

    • Fanjabi

      $(“:input:not([type=hidden])”).serializeArray();

  • http://kflorence.myopenid.com/ Kyle Florence

    Just in case anyone was wondering, this function supports all the new HTML 5 type attributes, as seen by the regex match they use:

    rinput = /color|date|datetime|email|hidden|month|number|password|range|search|tel|text|time|url|week/i

  • Najeeb Ahmd

    How i will get the posted values in process.php?

  • Lucas

    How I send the created array to my controller?
    Im using Java + jsp.
    And have some way to send the array with only the values and not the name?

  • Guest

    How about field values such as %, $ and &?

    When I try serializing these values I get an error :(

  • teo

    Regarding the example:

    $('form').submit(function() {
    alert($(this).serializeArray());
    return false;
    });

    Since there is an alert() statement in the example, the sentence “This produces the following data structure” may induce to think that that is actually what you will see in the alert dialog, while it will just show “[object Object]“

    • http://www.learningjquery.com/ Karl Swedberg

      Good point. I changed it to console.log, and added a note about browser support of that function.

  • http://www.learningjquery.com/ Karl Swedberg

    Good point. I changed it to console.log, and added a note about browser support of that function.

  • http://twitter.com/ArosME Arjen Oosterkamp

    The following code will serialize for use with JSON:

    (function( $ ){
    $.fn.serializeJSON=function() {
    var json = {};
    jQuery.map($(this).serializeArray(), function(n, i){
    json[n['name']] = n['value'];
    });
    return json;
    };
    })( jQuery );

    Simply use as $('form').serializeJSON();

  • Formyjuckmail

    once you have your form in a json object how do you grap values out of it? I was trying to use $.getJson() but theres no URL and I only want to find a single value from the Json