npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

saveform

v1.4.0

Published

Lightweight solution to persist form field values across page reloads

Readme

saveform

npm version license bundle size

Lightweight solution to persist form field values across page reloads

Automatically save HTML form field values to localStorage or sessionStorage and restore them when the user returns to the page.

  • Tiny footprint (~2KB minified and gzipped)
  • No dependencies
  • Modern ESM format
  • Works with all form elements (input, select, textarea, etc.)
  • Flexible field selection/exclusion
  • Custom storage triggers (input, change, or both)
  • Support for custom value accessors and setters
  • Configurable storage key prefix

Installation

Add this to your script:

import saveform from "saveform";

To use via CDN, add this to your HTML file:

<script type="importmap">
  {
    "imports": {
      "saveform": "https://cdn.jsdelivr.net/npm/saveform@1"
    }
  }
</script>

To use locally, install via npm:

npm install saveform

... and add this to your HTML file:

<script type="importmap">
  {
    "imports": {
      "saveform": "./node_modules/saveform/dist/saveform.min.js"
    }
  }
</script>

Usage

// One-liner: automatically save & restore form values with default settings
import saveform from "saveform";
saveform("#my-form");

// With options
saveform("#my-form", {
  storage: sessionStorage, // use sessionStorage instead of localStorage
  prefix: "myapp_", // prefix for storage keys. Default: "saveform_"
  events: ["change"], // only save on change events
});

On any form change, this persists all fields that have a name="..." or an id="..." in sessionStorage (default: localStorage) as a JSON object with the key ${prefix}${formId}, e.g. myapp_my-form. formId is the form's id= else name= else a random key.

By default, type="password" and type="file" fields are excluded. To include password fields, use:

saveform("#my-form", { exclude: '[type="file"]' });

API

saveform(element, options)

Creates a new saveform instance and immediately restores any saved values.

element

Type: HTMLElement | string

The form element or selector string.

options

Type: Object

| Option | Type | Default | Description | | ----------- | ---------------------- | ------------------------------------ | ------------------------------------ | | storage | Storage | localStorage | Storage mechanism to use | | prefix | string | 'saveform_' | Prefix for storage keys | | events | string[] | ['input', 'change'] | Events that trigger saving | | fields | string | Function | '*' | Fields to include (* includes all) | | exclude | string | Function | '[type="password"], [type="file"]' | Fields to exclude | | dropEmpty | boolean | false | Delete (reset) key if value is "" | | accessors | Object | {} | Custom value getters | | setters | Object | {} | Custom value setters |

Methods

Each saveform instance returns an object with these methods:

save()

Manually save current form values.

const form = saveform("#my-form");
form.save();

save() merges the current form values with any data already in storage. This ensures values from fields that were removed from the DOM (for example, in dynamic forms) remain intact.

restore()

Manually restore saved values.

form.restore();

clear()

Clear all saved values.

form.clear();

destroy()

Remove all event listeners and stop tracking.

form.destroy();

Field Selection

Control which fields are saved:

// Only save text and email inputs
saveform("#my-form", {
  fields: 'input[type="text"], input[type="email"]',
});

// Exclude specific fields
saveform("#my-form", {
  exclude: ".sensitive, [data-no-save]",
});

// Use functions for complex logic
saveform("#my-form", {
  fields: (field) => field.dataset.save === "true",
  exclude: (field) => field.name.startsWith("private_"),
});

Reset saved values

dropEmpty: true lets users revert to the page default by clearing a field.

// Allow user to reset saved valued
saveform("#my-form", { dropEmpty: true });

Custom Value Handlers

Define custom ways to get/set values:

saveform("#my-form", {
  accessors: {
    // Custom getter for tag inputs (comma-separated values)
    ".tag-input": (field) => field.value.split(",").map((t) => t.trim()),

    // Get selected options from multi-select
    "select[multiple]": (field) => Array.from(field.selectedOptions).map((opt) => opt.value),
  },

  setters: {
    // Custom setter for tag inputs
    ".tag-input": (field, value) => (field.value = Array.isArray(value) ? value.join(", ") : value),
  },
});

Development

git clone https://github.com/sanand0/saveform.git
cd saveform

npm install
npm run lint && npm run build && npm test

npm publish
git commit . -m"$COMMIT_MSG"; git tag $VERSION; git push --follow-tags

Release notes

  • 1.4.0: 29 Sep 2025. Add dropEmpty option that resets empty fields to page default
  • 1.3.1: 31 Jul 2025. Standardized package.json & README.md, move saveform.min.js into dist/
  • 1.2.2: 05 Jun 2025. Document how to save password / hidden fields, expand test coverage
  • 1.2.0: 22 May 2025. Fall back to id attribute if name is missing
  • 1.1.1: 21 May 2025. Renamed "all" option to "*". Added tests and docs
  • 1.0.0: 21 May 2025. Initial release

License

MIT