@f/serialize-form
v1.0.0
Published
Serialize a form to JSON
Downloads
545
Readme
serialize-form
Serialize a form to JSON
Installation
$ npm install @f/serialize-form
Usage
serialize-form takes a form element and turns it into a JSON object based on the names of the form controls. The naming conventions are defined by brackets.
Form syntax example:
serialize($(`
<form>
<input type='text' name='name[first]' value='micro' />
<input type='text' name='name[last]' value='js' />
<input type='checkbox' name='accepted_terms' checked />
</form>
`)) // -> {
name: {
first: 'micro',
last: 'js'
},
accepted_terms: true
}
Submit example:
var serialize = require('@f/serialize-form')
function onSubmit (e) {
var json = serialize(e.target)
return fetch('/user', {
method: 'post',
body: serialize(e.target)
})
}
API
serialize(form)
form
- The form element who's controls you want to serialize
Returns: The JSON serialization of form
License
MIT