1.0.0 • Published 8 years ago

@f/serialize-form v1.0.0

Weekly downloads
183
License
MIT
Repository
github
Last release
8 years ago

serialize-form

Build status Git tag NPM version Code style

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