All Projects → maxatwork → Form2js

maxatwork / Form2js

Licence: mit
Javascript library for collecting form data

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Form2js

Convform
A jQuery plugin that transforms a form into an interactive chat.
Stars: ✭ 141 (-77.62%)
Mutual labels:  jquery-plugin, form, jquery
Uploader
A lightweight and very configurable jQuery plugin for file uploading using ajax(a sync); includes support for queues, progress tracking and drag and drop.
Stars: ✭ 1,042 (+65.4%)
Mutual labels:  jquery-plugin, forms, jquery
Ziptastic Jquery Plugin
This is a jQuery plugin that shows how Ziptastic could be used.
Stars: ✭ 244 (-61.27%)
Mutual labels:  jquery-plugin, forms, jquery
Multipicker
Form styling plugin for jQuery
Stars: ✭ 90 (-85.71%)
Mutual labels:  jquery-plugin, form, jquery
Form
jQuery Form Plugin
Stars: ✭ 5,122 (+713.02%)
Mutual labels:  jquery-plugin, form, jquery
Jquery.redirect
jQuery Redirect Plugin
Stars: ✭ 182 (-71.11%)
Mutual labels:  jquery-plugin, form, jquery
Waitme
jquery plugin for easy creating loading css3/images animations
Stars: ✭ 302 (-52.06%)
Mutual labels:  jquery-plugin, form, jquery
Material Cards
Card style based on Google Material color palette
Stars: ✭ 370 (-41.27%)
Mutual labels:  jquery-plugin, jquery
Hc Sticky
JavaScript library that makes any element on your page visible while you scroll.
Stars: ✭ 375 (-40.48%)
Mutual labels:  jquery-plugin, jquery
React Hook Form
📋 React Hooks for form state management and validation (Web + React Native)
Stars: ✭ 24,831 (+3841.43%)
Mutual labels:  form, forms
Unform
Performance-focused API for React forms 🚀
Stars: ✭ 4,340 (+588.89%)
Mutual labels:  form, forms
Protip
A new generation jQuery Tooltip plugin
Stars: ✭ 357 (-43.33%)
Mutual labels:  jquery-plugin, jquery
Formik Persist
💾 Persist and rehydrate a Formik form to localStorage
Stars: ✭ 345 (-45.24%)
Mutual labels:  form, forms
Bootstrap Fileinput
An enhanced HTML 5 file input for Bootstrap 5.x/4.x./3.x with file preview, multiple selection, and more features.
Stars: ✭ 5,097 (+709.05%)
Mutual labels:  jquery-plugin, jquery
Datetimepicker
jQuery Plugin Date and Time Picker
Stars: ✭ 3,402 (+440%)
Mutual labels:  jquery-plugin, jquery
Gijgo
Gijgo - Free Javascript Controls
Stars: ✭ 424 (-32.7%)
Mutual labels:  jquery-plugin, jquery
Jquery Fullscreen Plugin
This jQuery plugin provides a simple to use mechanism to control the new fullscreen mode of modern browsers
Stars: ✭ 327 (-48.1%)
Mutual labels:  jquery-plugin, jquery
Bigslide.js
⚠️**DEPRECATED**⚠️ A tiny slide panel navigation jQuery plugin with big dreams
Stars: ✭ 415 (-34.13%)
Mutual labels:  jquery-plugin, jquery
Jcanvas
A jQuery plugin that makes the HTML5 canvas easy to work with.
Stars: ✭ 612 (-2.86%)
Mutual labels:  jquery-plugin, jquery
Peity
Progressive <svg> pie, donut, bar and line charts
Stars: ✭ 4,214 (+568.89%)
Mutual labels:  jquery-plugin, jquery

form2js

Convenient way to collect structured form data into JavaScript object. Example. Because everything is better with jQuery, jQuery plugin added, check out jquery.toObject.js. If you have any questions/suggestions, find out something weird or illogical - feel free to post an issue.

Warning! form2object.js and form2object function renamed to form2js.js and form2js respectively. Old names are in v1.0 tag.

Details

This is not a serialization library. Library used in example for JSON serialization is http://www.json.org/js.html Structure of resulting object defined by name attribute of form fields. See examples below. All this library does is collect form data and put it in a javascript object. Obviously you can get a JSON/XML/etc string by serializing it, but that's not its only purpose.

Usage

form2js(rootNode, delimiter, skipEmpty, nodeCallback, useIdIfEmptyName)

Values of all inputs under the rootNode will be collected into one object. skipping empty inputs if skipEmpty not false.

Objects/nested objects

Structure of resulting object defined in name attributes of form fields (or id if name is empty and useIdIfEmptyName parameter set to true). delimiter is "." (dot) by default, but can be changed.

<input type="text" name="person.name.first" value="John" />
<input type="text" name="person.name.last" value="Doe" />

becomes

{
  "person": {
    "name": {
      "first": "John",
      "last": "Doe"
    }
  }
}

Arrays

Several fields with the same name with brackets defines array of values.

<label><input type="checkbox" name="person.favFood[]" value="steak" checked="checked" /> Steak</label>
<label><input type="checkbox" name="person.favFood[]" value="pizza"/> Pizza</label>
<label><input type="checkbox" name="person.favFood[]" value="chicken" checked="checked" /> Chicken</label>

becomes

{
    "person": {
        "favFood": [ "steak", "chicken" ]
    }
}

Arrays of objects/nested objects

Same index means same item in resulting array. Index doesn't specify order (order of appearance in document will be used).

<dl>
    <dt>Give us your five friends' names and emails</dt>
    <dd>
        <label>Email <input type="text" name="person.friends[0].email" value="[email protected]" /></label>
        <label>Name <input type="text" name="person.friends[0].name" value="Smith Agent"/></label>
    </dd>
    <dd>
        <label>Email <input type="text" name="person.friends[1].email" value="[email protected]" /></label>
        <label>Name <input type="text" name="person.friends[1].name" value="Thomas A. Anderson" /></label>
    </dd>
</dl>

becomes

{
    "person" :
    {
        "friends" : [
            { "email" : "[email protected]", "name" : "Smith Agent" },
            { "email" : "[email protected]", "name" : "Thomas A. Anderson" }
        ]
    }
}

Rails-style notation

If array index starts with [a-zA-Z_], it will be treated as field of object.

<dl>
    <dt>Rails-style test</dt>
    <dd>
        <label>rails[field1][foo]<input type="text" name="rails[field1][foo]" value="baz" /></label>
        <label>rails[field1][bar]<input type="text" name="rails[field1][bar]" value="qux" /></label>
    </dd>
    <dd>
        <label>rails[field2][foo]<input type="text" name="rails[field2][foo]" value="baz" /></label>
        <label>rails[field2][bar]<input type="text" name="rails[field2][bar]" value="qux" /></label>
    </dd>
</dl>

will give us:

{
    "rails": {
        "field1": {
            "foo": "baz",
            "bar": "qux"
        },
        "field2": {
            "foo": "baz",
            "bar": "qux"
        }
    }
}

Custom fields

You can implement custom nodeCallback function (passed as 4th parameter to form2object()) to extract custom data:

<dl id="dateTest">
<dt>Date of birth:</dt>
<dd data-name="person.dateOfBirth" class="datefield">
	<select name="person.dateOfBirth.month">
		<option value="01">January</option>
		<option value="02">February</option>
		<option value="03">March</option>
		<option value="04">April</option>
		<option value="05">May</option>
		<option value="06">June</option>
		<option value="07">July</option>
		<option value="08">August</option>
		<option value="09">September</option>
		<option value="10">October</option>
		<option value="11">November</option>
		<option value="12">December</option>
	</select>
	<input type="text" name="person.dateOfBirth.day" value="1" />
	<input type="text" name="person.dateOfBirth.year" value="2011" />
</dd>
</dl>

<script type="text/javascript">
	function processDate(node)
	{
		var dataName = node.getAttribute ? node.getAttribute('data-name') : '',
		    dayNode,
		    monthNode,
		    yearNode,
		    day,
		    year,
		    month;

		if (dataName && dataName != '' && node.className == 'datefield')
		{
			dayNode = node.querySelector('input[name="'+dataName + '.day"]');
			monthNode = node.querySelector('select[name="'+dataName + '.month"]');
			yearNode = node.querySelector('input[name="'+dataName + '.year"]');

			day = dayNode.value;
			year = yearNode.value;
			month = monthNode.value;

			return { name: dataName, value:  year + '-' + month + '-' + day};
		}

		return false;
	}

	var formData = form2object('dateTest', '.', true, processDate);
</script>

using processDate() callback formData will contain

{
	"person": {
		"dateOfBirth": "2011-01-12"
	}
}

Why not .serializeArray()?

JQuery's .serializeArray() works a bit different. It makes this structure from markup in "Arrays of objects/nested objects" example:

[
    { "person.friends[0].email" : "[email protected]" },
    { "person.friends[0].name" : "Smith Agent" },
    { "person.friends[1].email" : "[email protected]" },
    { "person.friends[1].name" : "Thomas A. Anderson" }
]
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].