Form Saver
A handy little script that lets users save and reuse form data.
Download Form Saver / View the demo
"The Vanilla JS Guidebook" and level-up as a web developer. 🚀
Want to learn how to write your own vanilla JS plugins? Check outGetting Started
Compiled and production-ready code can be found in the dist
directory. The src
directory contains development code.
1. Include Form Saver on your site.
2. Add the markup to your HTML.
Text Input Text Input to Ignore Checkbox 1 Checkbox 2 Radio 1 Radio 2 Select 1 Select 2 Select 3 Save Form Data Delete Form Data
Create your forms as normal. All forms must have an ID, all form fields must have a name attribute, and checkboxes and radio buttons must also have a value
attribute, or they won't work properly with Form Saver. Use the [data-form-no-save]
data attribute to omit a form field from being saved.
You can use a
elements instead of buttons to save and delete data:
Save Form Data Delete Form Data
3. Initialize Form Saver.
In the footer of your page, after the content, initialize Form Saver. And that's it, you're done. Nice work!
Installing with Package Managers
You can install Form Saver with your favorite package manager.
- NPM:
npm install cferdinandi/form-saver
- Bower:
bower install https://github.com/cferdinandi/form-saver.git
- Component:
component install cferdinandi/form-saver
Working with the Source Files
If you would prefer, you can work with the development code in the src
directory using the included Gulp build system. This compiles, lints, and minifies code.
Dependencies
Make sure these are installed first.
Quick Start
- In bash/terminal/command line,
cd
into your project directory. - Run
npm install
to install required files. - When it's done installing, run one of the task runners to get going:
gulp
manually compiles files.gulp watch
automatically compiles files when changes are made and applies changes using LiveReload.
Options and Settings
Form Saver includes smart defaults and works right out of the box. But if you want to customize things, it also has a robust API that provides multiple ways for you to adjust the default options and settings.
Global Settings
You can pass options and callbacks into Form Saver through the init()
function:
formSaver;
Note: If you change the selectorSave
or selectorDelete
, you still need to include the [data-form-save]
and [data-form-delete]
attributes in order to pass in the selector for the form.
Override settings with data attributes
Form Saver lets you override global settings on a form-by-form basis using the [data-options]
data attribute:
Save Form Data Delete Form Data
Note: You must use valid JSON in order for the data-options
overrides to work.
Use Form Saver events in your own scripts
You can also call Form Saver events in your own scripts.
saveForm()
Save a form's data.
formSaver;
Example
var options = saveMessage: 'Your data has been saved. Booya!' ;formSaver;
deleteForm()
Delete a form's data.
formSaver;
Example
var btn = document;var formID = btnformid;formSaver;
loadForm()
Load a form's saved data.
formSaver;
Example
var forms = documentforms;for var i = formslength; i--; var form = formsi; formSaver;
destroy()
Destroy the current formSaver.init()
. This is called automatically during the init
function to remove any existing initializations.
formSaver;
Browser Compatibility
Form Saver works in all modern browsers, and IE 10 and above. You can extend browser support back to IE 9 with the classList.js polyfill.
Form Saver is built with modern JavaScript APIs, and uses progressive enhancement. If the JavaScript file fails to load, or if your site is viewed on older and less capable browsers, save and delete data buttons will not be displayed.
How to Contribute
In lieu of a formal style guide, take care to maintain the existing coding style. Don't forget to update the version number, the changelog (in the readme.md
file), and when applicable, the documentation.
License
The code is available under the MIT License.