Persist Form State To Web Storage On Submit – FormPersistence.js

Category: Form , Javascript | February 7, 2019
Author: FThompson
Views Total: 67
Official Page: Go to website
Last Update: February 7, 2019
License: MIT


Persist Form State To Web Storage On Submit – FormPersistence.js


The FormPersistence.js JavaScript library automatically stores the current form state to web storage that supports both localStorage and sessionStorage persistence.

How to use it:

Download and import the FormPersistence.js into the document.

<script src='form-persistence.js'></script>

Attach the FormPersistence.js to the target HTML form. This will automatically store the form data in the localStorage on form submit.

window.addEventListener('load', () => {
  let form = document.getElementById('myForm')

Store the form data using localStorage (default) or sessionStorage.

// default

// use sessionStorage, useSessionStorage)

Restore the form data on the next visit.

// default

// use sessionStorage
FormPersistence.load(form, useSessionStorage)

Clear the saved form data.

// default

// use sessionStorage
FormPersistence.clearStorage(form, useSessionStorage)

You Might Be Interested In:

Leave a Reply