$('#formid').serialize();
, nor do I want the map returned by $('#formid').serializeArray();
serializeArray
already does exactly that. You just need to massage the data into your required format:0
and '
. Array.prototype.reduce
because it's a one-liner, and it doesn't rely on Underscore.js or the like:Array.prototype.map
, but you don't need to clutter up your scope with an additional object variable. One-stop shopping.name
attributes are valid HTML, and is actually a common approach. Using any of the answers in this thread will be inappropriate in that case (since object keys must be unique).serializeArray()
method, but that does not include form elements that are disabled. We will often disable form elements that are 'sync'd' to other sources on the page, but we still need to include the data in our serialized object. So serializeArray()
is out. We used the :input
selector to get all input elements (both enabled and disabled) in a given container, and then $.map()
to create our object.name
attribute, which will be the name of the property of the resulting object.$.map()
function, and you have complete control over your selector (so, which elements you end up including in your resulting object). Also, no extra plugin required. Plain old jQuery.map
method. name[key]
, but it will generate like this <places>..</places>
XML element containing a list of places the user likes thus a list of <place>..</place>
elements each one containing for example a <name>..</name>
element, a <type>..</type>
element and then a list of <activity>..</activity>
elements to represent the activities you can perform in such a place. So your XML structure would be like this:<form>
tag is the root
, but then we have that <place>
element which is a container and not a data element itself, so we cannot use an input tag for it.<fieldset>
tag comes in handy! We'll use <fieldset>
tags to represent all container elements in our form/XML representation and so getting to a result like this:name='array[]'
like name inside the form and everything is pretty, simple and semantic..serialize()
and .serializeArray()
work great in most cases and are mostly intended to be used, but I think this whole idea of writing a form as an XML structure with meaningful names and converting it into a well-formed JSON object is worth the try, also the fact you can add same-name input tags without worrying is very useful if you need to retrive dynamically generated forms data.