Gets form data via form.elements
npm install get-form-data[![Travis][build-badge]][build]
[![npm package][npm-badge]][npm]
[![Coveralls][coveralls-badge]][coveralls]
Gets form data - or data for a named form field - via form.elements.
Data is retrieved in a format similar to request parameters which would be sent if the form was submitted, so this module is suitable for extracting form data on the client side for projects which implement isomorphic handling of form submission.
```
npm install get-form-data
Browser bundles area available, which export a global getFormData function.
* get-form-data.js (development version)
* get-form-data.min.js (compressed production version)
To get data for an entire form, use the getFormData() function:
`html
Do you want to use Express Shipping?
I have read and agree to the Terms of Service.
`javascript
let form = document.querySelector('#productForm')let data = getFormData(form)
console.log(JSON.stringify(data))
`
`json
{"product": "1", "quantity": "9", "shipping": "express", "tos": true}
`$3
To get data for individual form fields (which may contain multiple form inputs with the same name), use the
getFieldData() function, which is exposed as a property of getFormData:`html
`
`javascript
let form = document.querySelector('#tshirtForm')let sizes = getFormData.getFieldData(form, 'sizes')
console.log(JSON.stringify(sizes))
`
`
["M", "L"]
`$3
To trim user input, pass a
trim: true option to getFormData() or getFieldData():`html
`
`javascript
let form = document.querySelector('#signupForm')let data = getFormData(form, {trim: true})
console.log(JSON.stringify(data))
`
`
{"username": "AzureDiamond", "password": "hunter2"}
`$3
Disabled inputs are ignored by default; to include their data, pass an
includeDisabled: true option to getFormData() or getFieldData().`javascript
let data = getFormData(form, {includeDisabled: true})
`$3
Where possible, data extracted from
will be native
File objects.If the
.files property is not available, the .value property will be used to provide data instead.API
$3
Extracts data from a