Skip to content

Plain JavaScript method to convert a HTML form (fields and values) to JavaScript (multidimensional) object.

License

Notifications You must be signed in to change notification settings

krismeister/formToObject.js

 
 

Repository files navigation

formToObject.js

Build Status NPM package NPM downloads Dependency Status

Convert HTML forms with all their fields and values to multi-dimensional JavaScript objects

How to use
<!-- Include minified script (~2kb) -->
<script src="build/formToObject.min.js"></script>

Using the DOM node id

var myFormObj = formToObject('myFormId');
/* 
  console.log(myFormObj);
  {
    saveSettings: 'Save',
    name: 'Serban',
    race: 'orc',
    settings: {
       input: 'keyboard',
       video: {
          resolution: '1024x768',
          vsync: 'on'
       }
    }
  }
*/

Using the actual DOM Node

var $formNode = document.getElementById('myFormId');
var myFormObj = formToObject($formNode);
console.log(myFormObj);

Sending form data server-side

$.ajax({
  'url': '/app/settings/save/',
  'type': 'post',
  'data': formToObject('saveSettingsForm'),
  'success': function(r){}
});
Browser support

IE 8, Firefox 3.5, Chrome, Safari, Opera 10, every mobile browser.

Screenshot

About

Plain JavaScript method to convert a HTML form (fields and values) to JavaScript (multidimensional) object.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 71.2%
  • HTML 28.8%