Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

form_to_object

Package Overview
Dependencies
Maintainers
1
Versions
13
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

form_to_object

Convert a HTML form to an a JavaScript plain object (multi-dimensional).

  • 3.1.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
845
increased by0.48%
Maintainers
1
Weekly downloads
 
Created
Source

form-to-object (formToObject.js)

Convert HTML forms with all their fields and values to multidimensional JavaScript objects

Workflow status Codecov branch Latest tag npm version npm downloads

Install

As a npm package:

npm install form_to_object
import formToObject from 'form_to_object';
// or
const formToObject = require('form_to_object');

As a JS script:

<!-- Include minified script (~6kb) -->
<script src="build/formToObject.js"></script>

<!-- jsdelivr (CDN) -->
<script src="
https://cdn.jsdelivr.net/npm/form_to_object@3.0.0/build/bundle/formToObject.min.js
"></script>

Example

  • Using the DOM node id: formToObject('myFormId');
  • Using the actual DOM Node reference: formToObject(document.getElementById('myFormId'));

Resulted value:

{
  "saveSettings": "Save",
  "name": "Serban",
  "race": "orc",
  "settings": {
     "input": "keyboard",
     "video": {
        "resolution": "1024x768",
        "vsync": "on"
     }
  }
}

Good to know:

  1. If <form> fields are found, but they lack of name attribute property, the result will be {} (empty object).
  2. If <form> contains only disabled fields, the result will be {} (empty object). If you force includeDisabledFields then key:value pairs will be returned.
  3. An empty <form> will throw an Error.
  4. In case of an error like non-existing form or invalid selector, an Error will be thrown.

Options

Option nameDefaultDescription
includeEmptyValuedElementsboolean (default false)Return field names as keys with empty value "" instead of just ignoring them.
w3cSuccessfulControlsOnlyboolean (default false)TBA, WIP
selectNameWithEmptyBracketsReturnsArrayboolean (default true)<select> field names like name="select[]" always return an array [a,b] instead or array of arrays [0: [a,b]].
checkBoxNameWithEmptyBracketsReturnsArrayboolean (default true)<input> checkboxes with field names like name=checkbox[] always return an array [a,b] instead or array of arrays [0: [a,b]].

Browser support

IE 8, Firefox 3.5, Chrome, Safari, Opera 10, every mobile browser.

Example

Keywords

FAQs

Package last updated on 09 Jan 2024

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc