All Projects → cferdinandi → form-saver

cferdinandi / form-saver

Licence: MIT license
A simple script that lets users save and reuse form data.

Programming Languages

javascript
184084 projects - #8 most used programming language
CSS
56736 projects

Projects that are alternatives of or similar to form-saver

Validate
A lightweight form validation script.
Stars: ✭ 227 (+238.81%)
Mutual labels:  forms, vanilla-js, no-dependencies
X Ray
X-Ray is a script that lets users toggle password visibility in forms.
Stars: ✭ 40 (-40.3%)
Mutual labels:  vanilla-js, no-dependencies
Vanilla Emoji Picker
Modern emoji picker. Super light 2kb gzipped, simple and no frameworks 😻
Stars: ✭ 20 (-70.15%)
Mutual labels:  vanilla-js, no-dependencies
Houdini
A simple, accessible show-and-hide/accordion script.
Stars: ✭ 148 (+120.9%)
Mutual labels:  vanilla-js, no-dependencies
Atomic
A tiny, Promise-based vanilla JS Ajax/HTTP plugin with great browser support.
Stars: ✭ 526 (+685.07%)
Mutual labels:  vanilla-js, no-dependencies
Smooth Scroll
A lightweight script to animate scrolling to anchor links.
Stars: ✭ 5,350 (+7885.07%)
Mutual labels:  vanilla-js, no-dependencies
Right Height
Dynamically set content areas of different lengths to the same height.
Stars: ✭ 91 (+35.82%)
Mutual labels:  vanilla-js, no-dependencies
Modals
Simple modal dialogue windows
Stars: ✭ 85 (+26.87%)
Mutual labels:  vanilla-js, no-dependencies
Swiped Events
Adds `swiped` events to the DOM in 0.7k of pure JavaScript
Stars: ✭ 249 (+271.64%)
Mutual labels:  vanilla-js, no-dependencies
Materialstartpage
Save your favorite sites with stylish using Local Storage
Stars: ✭ 12 (-82.09%)
Mutual labels:  localstorage, vanilla-js
Bin
A tiny (<1kb) localStorage and sessionStorage helper library.
Stars: ✭ 70 (+4.48%)
Mutual labels:  localstorage, vanilla-js
Tabby
Lightweight, accessible vanilla JS toggle tabs.
Stars: ✭ 449 (+570.15%)
Mutual labels:  vanilla-js, no-dependencies
tabbis.js
Pure vanilla javascript tabs with nesting
Stars: ✭ 44 (-34.33%)
Mutual labels:  vanilla-js, no-dependencies
Gumshoe
A simple vanilla JS scrollspy script.
Stars: ✭ 640 (+855.22%)
Mutual labels:  vanilla-js, no-dependencies
Mailtoui
A simple way to enhance your mailto links with a convenient user interface.
Stars: ✭ 162 (+141.79%)
Mutual labels:  vanilla-js, no-dependencies
Formio.js
JavaScript powered Forms with JSON Form Builder
Stars: ✭ 1,060 (+1482.09%)
Mutual labels:  forms, vanilla-js
Formio
A Form and Data Management Platform for Progressive Web Applications.
Stars: ✭ 1,245 (+1758.21%)
Mutual labels:  forms, vanilla-js
react-local-storage-cache
How to cache results in React with Local Storage.
Stars: ✭ 27 (-59.7%)
Mutual labels:  localstorage
Covid19-Tracker
The situation of the Coronavirus epidemic data around the World and details info in Vietnam.
Stars: ✭ 15 (-77.61%)
Mutual labels:  localstorage
acc toolbar
Accessibility Toolbar Plugin
Stars: ✭ 42 (-37.31%)
Mutual labels:  no-dependencies

Form Saver Build Status

A handy little script that lets users save and reuse form data.

Download Form Saver / View the demo


Want to learn how to write your own vanilla JS plugins? Check out "The Vanilla JS Guidebook" and level-up as a web developer. 🚀


Getting 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.

<link rel="stylesheet" href="dist/css/form-saver.css">
<script src="dist/js/form-saver.js"></script>

2. Add the markup to your HTML.

<form id="form-id">
	<div>
		<label>Text Input</label>
		<input name="input" type="text">
	</div>

	<div>
		<label>Text Input to Ignore</label>
		<input data-form-no-save name="input-ignore" type="text">
	</div>

	<div>
		<label>
			<input type="checkbox" name="checkbox1" value="1">
			Checkbox 1
		</label>
	</div>

	<div>
		<label>
		<input type="checkbox" name="checkbox2" value="2">
			Checkbox 2
		</label>
	</div>

	<div>
		<label>
		<input type="radio" name="radioset" value="radio1">
			Radio 1
		</label>
	</div>

	<div>
		<label>
		<input type="radio" name="radioset" value="radio2">
			Radio 2
		</label>
	</div>

	<div>
		<select name="select">
			<option>Select 1</option>
			<option>Select 2</option>
			<option>Select 3</option>
		</select>
	</div>

	<div>
		<textarea name="textarea"></textarea>
	</div>

	<div class="form-saver">
		<div data-form-status></div>
		<div>
			<button data-form-save="#form-id">
				Save Form Data
			</button>
			<button data-form-delete="#form-id">
				Delete Form Data
			</button>
		</div>
	</div>
</form>

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:

<a data-form-save="#form-id" href="#">
	Save Form Data
</a>
<a data-form-delete="#form-id" href="#">
	Delete Form Data
</a>

3. Initialize Form Saver.

<script>
	formSaver.init();
</script>

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

  1. In bash/terminal/command line, cd into your project directory.
  2. Run npm install to install required files.
  3. 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.init({
	selectorStatus: '[data-form-status]', // Selector for the status container (must be a valid CSS selector)
	selectorSave: '[data-form-save]', // Selector for the save button (must be a valid CSS selector)
	selectorDelete: '[data-form-delete]', // Selector for the delete button (must be a valid CSS selector)
	selectorIgnore: '[data-form-no-save]', // Selector for fields to ignore (must be a valid CSS selector)
	deleteClear: true, // Boolean. Reload the page after deleting form data.
	saveMessage: 'Saved!', // Message to display when form data is successfully saved.
	deleteMessage: 'Deleted!', // Message to display when form data is successfully deleted.
	saveClass: '', // Class to add to save success message <div>
	deleteClass: '', // Class to add to delete success message <div>
	initClass: 'js-form-saver', // Class added to `<html>` element when initiated
	callbackSave: function ( btn, form ) {}, // Function to run after a form is saved
	callbackDelete: function ( btn, form ) {}, // Function to run after a form is deleted
	callbackLoad: function ( form ) {} // Function to run after form data is loaded from storage
});

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:

<button
	data-form-save
	data-options='{
					"saveMessage": "Saved!",
					"saveClass": "alert-success"
	              }'
>
	Save Form Data
</button>

<button
	data-form-delete
	data-options='{
					"deleteMessage": "Deleted!",
					"deleteClass": "alert-success",
					"deleteClear": true
	              }'
>
	Delete Form Data
</button>

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.saveForm(
	btn, // Node that saves the form. ex. document.querySelector('#save-btn')
	formID, // ID of the form to save. ex. #form-id
	options, // Classes and callbacks. Same options as those passed into the init() function.
	event // Optional, if a DOM event was triggered.
);

Example

var options = { saveMessage: 'Your data has been saved. Booya!' };
formSaver.saveForm( null, '#form', options );

deleteForm()

Delete a form's data.

formSaver.deleteForm = function (
	btn, // Node that deletes the form. ex. document.querySelector('#delete-btn')
	formID, // ID of the form to save. ex. #form-id
	options, // Classes and callbacks. Same options as those passed into the init() function.
	event // Optional, if a DOM event was triggered.
);

Example

var btn = document.querySelector('[data-form-delete]');
var formID = btn.form.id;
formSaver.deleteForm( btn, formID );

loadForm()

Load a form's saved data.

formSaver.loadForm = function (
	form, // Form node to delete. ex. document.querySelector('#form')
	options // Classes and callbacks. Same options as those passed into the init() function.
);

Example

var forms = document.forms;
for (var i = forms.length; i--;) {
	var form = forms[i];
	formSaver.loadForm( form );
}

destroy()

Destroy the current formSaver.init(). This is called automatically during the init function to remove any existing initializations.

formSaver.destroy();

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.

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].