Security News
tea.xyz Spam Plagues npm and RubyGems Package Registries
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
@alvinsj/form-serialize
Advanced tools
Readme
serialize form fields to submit a form over ajax
npm install form-serialize
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' }
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 |
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 |
Serializers take 3 arguments: result
, key
, value
and should return a newly updated result.
See the example serializers in the index.js source file.
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
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"
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"
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' ] }
This module is based on ideas from jQuery serialize and the Form.serialize method from the prototype library
MIT
FAQs
serialize html forms
The npm package @alvinsj/form-serialize receives a total of 2 weekly downloads. As such, @alvinsj/form-serialize popularity was classified as not popular.
We found that @alvinsj/form-serialize demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
Security News
As cyber threats become more autonomous, AI-powered defenses are crucial for businesses to stay ahead of attackers who can exploit software vulnerabilities at scale.
Security News
UnitedHealth Group disclosed that the ransomware attack on Change Healthcare compromised protected health information for millions in the U.S., with estimated costs to the company expected to reach $1 billion.