form_to_object
v4.0.0
Published
Convert a HTML form to an a JavaScript plain object (multi-dimensional).
Maintainers
Readme
form-to-object
formToObject() reads the fields of an HTML form and returns one JavaScript object.
Brackets and dots in the field names give objects and arrays in the result. Examples of such names are user[address][zip], tags[] and customer.address.zip.

Installation
Install the npm package:
npm install form_to_objectImport the function:
import formToObject from 'form_to_object'; // or const formToObject = require('form_to_object');
As an alternative, you can get the script from a CDN. The script adds the global function formToObject.
<script src="https://cdn.jsdelivr.net/npm/[email protected]/build/bundle/formToObject.min.js"></script>Example
<form id="userForm">
<input name="user[name]" value="Serban">
<input name="user[tags][]" value="js">
<input name="user[tags][]" value="html">
<input type="checkbox" name="newsletter">
</form>formToObject('#userForm');
// or
formToObject(document.getElementById('userForm'));The function returns this object:
{
"user": {
"name": "Serban",
"tags": ["js", "html"]
},
"newsletter": false
}Documentation
- The documentation gives all the rules for field names, checkboxes, radio buttons, options and TypeScript.
- The changelog gives the changes from version 3 to version 4. It is necessary to read these changes before you install version 4 in a project that uses version 3.
- CONTRIBUTING.md tells you how to do the tests.
