Convert an HTML form field to a JSON object with inner objects.

Posted by Tawani on Stack Overflow See other posts from Stack Overflow or by Tawani
Published on 2010-03-31T12:29:24Z Indexed on 2010/03/31 12:33 UTC
Read the original article Hit count: 502

Filed under:
|
|
|
|

Given the following HTML form:

<form id="myform">
  Company: <input type="text" name="Company" value="ACME, INC."/>
  First Name: <input type="text" name="Contact.FirstName" value="Daffy"/>
  Last Name: <input type="text" name="Contact.LastName" value="Duck"/>
</form>

What is the best way serialize this form in javascript to a JSON object in the format:

{
  Company:"ACME, INC.",
  Contact:{FirstName:"Daffy", LastName:"Duck"}
}

Also note that there might be more than 1 "." sign in the field name.

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about jQuery