{"name":"serializeArray","type":"method","title":".serializeArray()","deprecated":null,"removed":null,"desc":"Encode a set of form elements as an array of names and values.","categories":["forms","ajax/helper-functions","version/1.2"],"entries":[{"return":"Array","signatures":{"added":"1.2"},"examples":{"desc":"Get the values from a form, iterate through them, and append them to a results display.","code":"\n function showValues() {\n var fields = $( \":input\" ).serializeArray();\n $( \"#results\" ).empty();\n jQuery.each( fields, function( i, field ) {\n $( \"#results\" ).append( field.value + \" \" );\n });\n }\n\n $( \":checkbox, :radio\" ).click( showValues );\n $( \"select\" ).change( showValues );\n showValues();\n","css":"\n body, select {\n font-size: 14px;\n }\n form {\n margin: 5px;\n }\n p {\n color: red;\n margin: 5px;\n }\n b {\n color: blue;\n }\n","html":"\n
Results:
\n\n"},"longdesc":"\nThe .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:
\n<form>\n <div><input type=\"text\" name=\"a\" value=\"1\" id=\"a\"></div>\n <div><input type=\"text\" name=\"b\" value=\"2\" id=\"b\"></div>\n <div><input type=\"hidden\" name=\"c\" value=\"3\" id=\"c\"></div>\n <div>\n <textarea name=\"d\" rows=\"8\" cols=\"40\">4</textarea>\n </div>\n <div><select name=\"e\">\n <option value=\"5\" selected=\"selected\">5</option>\n <option value=\"6\">6</option>\n <option value=\"7\">7</option>\n </select></div>\n <div>\n <input type=\"checkbox\" name=\"f\" value=\"8\" id=\"f\">\n </div>\n <div>\n <input type=\"submit\" name=\"g\" value=\"Submit\" id=\"g\">\n </div>\n</form>\n \n 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:
\n$( \"form\" ).submit(function( event ) {\n console.log( $( this ).serializeArray() );\n event.preventDefault();\n});\n \n This produces the following data structure (provided that the browser supports console.log):
\n[\n {\n name: \"a\",\n value: \"1\"\n },\n {\n name: \"b\",\n value: \"2\"\n },\n {\n name: \"c\",\n value: \"3\"\n },\n {\n name: \"d\",\n value: \"4\"\n },\n {\n name: \"e\",\n value: \"5\"\n }\n]\n \n "}]}