How to convert form data to object using MooTools

Aldie picture Aldie · Jan 30, 2010 · Viewed 9.1k times · Source

I would like to convert an entire form of data to a javascript object.

<form id='myform'>
   <input type='text' name='field1' value='foo'>
   <input type='text' name='field2' value='bar'>
</form>

would convert to a javascript object...

{
   field1: 'foo',
   field2: 'bar'
}

Answer

Trebla picture Trebla · Dec 31, 2010

In MooTools you can do easy trick how to convert all forms values into the Object:

var formObjects=$('myform').toQueryString().parseQueryString();

Convert to JSON:

var formJson=JSON.encode(formObjects);