@f/serialize-form

1.0.0 • Public • Published

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

/@f/serialize-form/

    Package Sidebar

    Install

    npm i @f/serialize-form

    Weekly Downloads

    153

    Version

    1.0.0

    License

    MIT

    Last publish

    Collaborators

    • f