• Stars
    star
    399
  • Rank 103,974 (Top 3 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 10 years ago
  • Updated over 4 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

serialize html forms

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 successful 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"

indexed arrays

Adding numbers between brackets for the array notation above will result in a hash serialization with explicit ordering based on the index number regardless of element ordering.

Like the "explicit array fields" this does not affect url-encoding mode output in any way.

<form id="todos-form">
	<input type="text" name="todos[1]" value="milk" />
	<input type="text" name="todos[0]" value="eggs" />
	<input type="text" name="todos[2]" value="flour" />
</form>
var serialize = require('form-serialize');
var form = document.querySelector('#todos-form');

var obj = serialize(form, { hash: true });
// obj -> { todos: ['eggs', 'milk', 'flour'] }

var str = serialize(form);
// str -> "todos[1]=milk&todos[0]=eggs&todos[2]=flour"

nested objects

Similar to the indexed array notation, attribute names can be added by inserting a string value between brackets. The notation can be used to create deep objects and mixed with the array notation.

Like the "explicit array fields" this does not affect url-encoding mode output.

<form id="nested-example">
	<input type="text" name="foo[bar][baz]" value="qux" />
	<input type="text" name="foo[norf][]" value="item 1" />
</form>
var serialize = require('form-serialize');
var form = document.querySelector('#todos-form');

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

references

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

license

MIT

More Repositories

1

zuul

[UNMAINTAINED] multi-framework javascript browser testing
JavaScript
961
star
2

package-browser-field-spec

Spec document for the 'browser' field in package.json
427
star
3

node-url

node.js core url module as a module
JavaScript
374
star
4

ansible-coreos-bootstrap

[NOT MAINTAINED] bootstrap a coreos machine for control via ansible
Python
227
star
5

npm-css

Require css from npm
JavaScript
128
star
6

badginator

badges for your badges
JavaScript
117
star
7

libuv.js

libuv javascript bindings
JavaScript
114
star
8

node-process

process information for node.js and browsers
JavaScript
111
star
9

qr.js

javascript qrcode library
JavaScript
92
star
10

node-enchilada

middleware for on-demand javascript bundling
JavaScript
74
star
11

node-required

identifies which modules your script is using
JavaScript
66
star
12

virtualbox-pxe-boot

Bootstrap for testing PXE boot configs in VirtualBox
66
star
13

bitcoin-address

bitcoin address verification and other tools
JavaScript
62
star
14

sequelize-encrypted

Encrypted fields for Sequelize ORM
JavaScript
60
star
15

fixjs

Financial Information Exchange protocol in javascript
JavaScript
55
star
16

npm-github

npm registry proxy to github repositories
JavaScript
54
star
17

changelog

changelog release management tool
JavaScript
51
star
18

docket

build docker images with secrets
Python
48
star
19

localenv

load environment variables from .env files
JavaScript
35
star
20

dom-events

DOM event binding and emitting
JavaScript
35
star
21

away

monitor when a user is inactive on a page
JavaScript
35
star
22

coreos-ansible-example

examples for using Ansible with CoreOS
Ruby
34
star
23

synthetic-dom-events

create DOM events for builtin event types
JavaScript
33
star
24

num

arbitrary size and precision number library in pure javascript
JavaScript
33
star
25

jetson-nano-image-maker

Create sd-card ready jetson nano images using Docker and Github Actions
Shell
29
star
26

node-book

fast and flexible logging for node.js
JavaScript
27
star
27

bamboo

javascript model library for basic CRUD
JavaScript
23
star
28

node-superstack

long stack traces for node.js
JavaScript
22
star
29

typeahead

bootstrap compatible typeahead web component
JavaScript
21
star
30

node-spaceport

decentralized service registry for nodejs
JavaScript
21
star
31

handlebars-extend-block

create extend blocks for handlebars
JavaScript
21
star
32

node-script

[DEPRECATED] see readme
JavaScript
19
star
33

node-presentation

c++ modules with node.js presentation and support material
C++
18
star
34

tryme

interactive code demo, documentation, and editor for github repositories
JavaScript
15
star
35

node-stackback

return array of v8 CallSite objects for a stacktrace
JavaScript
14
star
36

node-influx-collector

influxdb stats collector
JavaScript
12
star
37

qr-element

qrcode html element
JavaScript
12
star
38

node-htmltree

simple dom for xml/html
JavaScript
12
star
39

node-yummy

cookie session middleware for connect
JavaScript
12
star
40

int

arbitrary size integer library in javascript
JavaScript
12
star
41

udp-portal

tunnel UDP traffic over TCP
JavaScript
10
star
42

expressjs-tips-tricks

JavaScript
10
star
43

expando

expand/collapse html elements with variable height
JavaScript
10
star
44

docserv

documentation browser for node projects
JavaScript
9
star
45

chain-js

callback chaining library for javascript
JavaScript
8
star
46

ratelimit-middleware

ratelimit middleware for express
JavaScript
8
star
47

browser-stacks

stackframes from various browsers
JavaScript
7
star
48

intro-node-js

quick and dirty presentation of some node.js basics
JavaScript
7
star
49

reunion

JavaScript
6
star
50

socket.io-cloud

hosted socket.io (work in progress)
CSS
6
star
51

clock-input

clock-like time input control
JavaScript
6
star
52

node-jsrender

[NOT MAINTAINED] Node.js port of jsrender
JavaScript
6
star
53

react-webpack-boilerplate

Boilerplate example with react (with hot reloading), webpack, eslint, es6 (babel), and basic dockerfile.
JavaScript
6
star
54

influxdb-docker-stats

docker stats collection into influxdb
JavaScript
5
star
55

balabolka

dead simple hosted chat for your website
JavaScript
5
star
56

chrome-socket

streaming socket interface for chrome tcp
JavaScript
5
star
57

polyfill-middleware

selectively serve javascript polyfills based on user-agent
JavaScript
5
star
58

node-book-git

git middleware for book logging framework
JavaScript
4
star
59

etcd-spaceport

service registry leveraging etcd as a backend
JavaScript
4
star
60

node-taters

super hash powers for express view rendering
JavaScript
4
star
61

etcdjs-watch

watch etcd keys for changes
JavaScript
4
star
62

hubitat-somfy-mylink

Somfy MyLink Integration for Hubitat
Groovy
3
star
63

node-book-file

file transport for book logging framework
JavaScript
3
star
64

node-partial-compare

deep partial comparison of objects
JavaScript
3
star
65

node-lsmod

get a list of modules and versions for the main prog
JavaScript
3
star
66

node-bookrc

automatic config loading for the book logging framework
JavaScript
3
star
67

logspout-firehose

Logspout adapter for writing Docker container logs to AWS Kinesis Firehose
Go
3
star
68

node-connect-raven

connect error handling middleware to log via raven
JavaScript
3
star
69

events-browserify

DEPRECATED - use https://github.com/Gozala/events
JavaScript
3
star
70

dom.position

dom position functions
JavaScript
3
star
71

node-filepile

file backed worker queue
JavaScript
3
star
72

node-veto

middleware to error check parameters
JavaScript
3
star
73

flip-counter

apple style flip counter
JavaScript
3
star
74

node-weaklink

hunt down and eradicate floating dependencies
JavaScript
2
star
75

node-mandible

html and txt email templates with handlebars
JavaScript
2
star
76

node-book-pagerduty

pagertudy notifier for panic logs
JavaScript
2
star
77

qr

stdout into qr code
JavaScript
2
star
78

takeoff

cross platform onscreen takeoff estimating application
C++
2
star
79

fx.js

basic dom transitions
JavaScript
2
star
80

bootstrap-confirm

confirmation dialog with bootstrap ui classes
JavaScript
2
star
81

node-sauron

nodejs process manager and launcher
JavaScript
2
star
82

dotfiles

home directory config files
Shell
2
star
83

studio-extension-ogv-example

TypeScript
2
star
84

node-jsbundler

RENAMED
2
star
85

microtime2iso

convert decimal time values to iso8601
JavaScript
2
star
86

node-asset-manager

DEPRECATED!! don't use this shit! see readme
JavaScript
2
star
87

node-book-email

email transport for the book logging framework
JavaScript
2
star
88

node-abe

Expose ArrayBuffers as read/write text streams
JavaScript
2
star
89

d3-examples

collection of d3 examples for use with tryme
JavaScript
2
star
90

braces

ctemplate and mustache inspired templates
JavaScript
2
star
91

xrequest

cross browser ajax request
JavaScript
2
star
92

npm-install

recursive installation of npm packages
JavaScript
2
star
93

fpddir

Federal Reserve E-Payments Routing Directory
JavaScript
2
star
94

node-crumbs

[deprecated] check out my node-cookie repo for basic cookie (de)serialization instead
JavaScript
2
star
95

mailview

preview html and txt emails
JavaScript
1
star
96

stringencoding

Encode to/from Typed Array buffers
JavaScript
1
star
97

eyersee

JavaScript
1
star
98

book-bugsnag

bugsnag logger for book
JavaScript
1
star
99

node-v8-bypass

Bypass the v8 heap limit by storing cache objects in node process memory
C++
1
star
100

combobox

Lightweight select box replacement
JavaScript
1
star