How do I convert a standard HTML form to JSON representation that includes all data?
<p>I have a simple HTML form:</p>

<pre><code>&lt;form id="formid1" action="#" method="post"&gt;
                       &lt;table&gt;
                        &lt;tr&gt;&lt;td&gt; First name: &lt;/td&gt;&lt;td&gt;&lt;input type="text" name="firstname" id="firstname"/&gt;  &lt;/td&gt;&lt;/tr&gt;
                        &lt;tr&gt;&lt;td&gt;  Last name: &lt;/td&gt;&lt;td&gt;&lt;input type="text" name="lastname" id="lastname"/&gt;  &lt;/td&gt;&lt;/tr&gt;
                        &lt;tr&gt;&lt;td&gt; Address:  &lt;/td&gt;&lt;td&gt; &lt;input type="text" name="address" id="address"/&gt; &lt;/td&gt;&lt;/tr&gt;
                        &lt;tr&gt;&lt;td&gt; Zip: &lt;/td&gt;&lt;td&gt; &lt;input type="text" name="zip" id="zip"/&gt; &lt;/td&gt;&lt;/tr&gt;
                        &lt;tr&gt;&lt;td&gt; Sex:  &lt;/td&gt;&lt;td&gt; &lt;input type="radio" name="sex" value="male" /&gt; Male&lt;br /&gt;
                                     &lt;input type="radio" name="sex" value="female" /&gt; Female&lt;br /&gt;&lt;/td&gt;&lt;/tr&gt;

                        &lt;tr&gt;&lt;td&gt;  &lt;/td&gt;&lt;td&gt;  &lt;input type="submit" value="Submit" /&gt;&lt;/td&gt;&lt;/tr&gt;

                        &lt;/table&gt;
                          &lt;/form&gt;
</code></pre>

<p>I need to get a JSON representation of the Form elements including id, value, type, form action, form id etc. </p>

<p>I tried playing with the following code but I am not getting what I am looking for:</p>

<pre><code>&lt;script&gt;
$(document).ready(function(){

    var encoded = $.toJSON($('#formid1')); 

    $("#formid1").submit(function() {
  $.colorbox({html:'&lt;p&gt;Form Converted to JSON Data: &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;/p&gt;'+ encoded  });
      return false;
    });

    });
&lt;/script&gt;
</code></pre>

<p>I get: </p>

<p>{"length":1,"0":{"firstname":{},"lastname":{},"address":{},"zip":{},"sex":{"0":{},"1":{}},"6":{}},"context":{"jQuery16108216209608688556":1},"selector":"#formid1"}
close</p>
