Code Monkey home page Code Monkey logo

form-serialize's Introduction

form-serialize Build Status

serialize form fields to submit a form over ajax

install

npm install form-serialize

use

form-serialize supports two output formats, url encoded (default) or hash (js objects).

Lets serialize the following html form:

<form id="example-form">
	<input type="text" name="foo" value="bar"/>
	<input type="submit" value="do it!"/>
</form>
var serialize = require('form-serialize');
var form = document.querySelector('#example-form');

var str = serialize(form);
// str -> "foo=bar"

var obj = serialize(form, { hash: true });
// obj -> { foo: 'bar' }

api

serialize(form [, options])

Returns a serialized form of a HTMLForm element. Output is determined by the serializer used. Default serializer is url-encoded.

arg type desc
form HTMLForm must be an HTMLForm element
options Object optional options object

options

option type default desc
hash boolean false if true, the hash serializer will be used for serializer option
serializer function url-encoding override the default serializer (hash or url-encoding)
disabled boolean false if true, disabled fields will also be serialized
empty boolean false if true, empty fields will also be serialized

custom serializer

Serializers take 3 arguments: result, key, value and should return a newly updated result.

See the example serializers in the index.js source file.

notes

only successfull control form fields are serialized (with the exception of disabled fields if disabled option is set)

multiselect fields with more than one value will result in an array of values in the hash output mode using the default hash serializer

explicit array fields

Fields who's name ends with [] are always serialized as an array field in hash output mode using the default hash serializer. The field name also gets the brackets removed from its name.

This does not affect url-encoding mode output in any way.

<form id="example-form">
	<input type="checkbox" name="foo[]" value="bar" checked />
	<input type="checkbox" name="foo[]" value="baz" />
	<input type="submit" value="do it!"/>
</form>
var serialize = require('form-serialize');
var form = document.querySelector('#example-form');

var obj = serialize(form, { hash: true });
// obj -> { foo: ['bar'] }

var str = serialize(form);
// str -> "foo[]=bar"

references

This module is based on ideas from jQuery serialize and the Form.serialize method from the prototype library

license

MIT

form-serialize's People

Contributors

barwin avatar defunctzombie avatar imhoffd avatar izolate avatar jxson avatar phillipj avatar rogaldh avatar santigimeno avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.