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

Preview:

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

Description:

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')
  FormPersistence.persist(form)
})

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

// default
FormPersistence.save(form)

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

Restore the form data on the next visit.

// default
FormPersistence.load(form)

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

Clear the saved form data.

// default
FormPersistence.clearStorage(form)

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

You Might Be Interested In:


Leave a Reply