@robmarr/intercept-form-submits

Call a callback if a form was submitted

Usage no npm install needed!

<script type="module">
  import robmarrInterceptFormSubmits from 'https://cdn.skypack.dev/@robmarr/intercept-form-submits';
</script>

README

Intercept Form Submits

This module is mainly to intercept applicable form submits for a single-page app router. The options are geared toward intercepting internal links that should change the page state in some way.

Install

npm i intercept-form-submits

Usage

var interceptSubmits = require('intercept-form-submits');

interceptSubmits(function(e, el) {
  // Change the page state here
  // `e` is the event object
  // `el` is the submitted form, which might be different from `e.target`
});

A more advanced usage is to pass options and an optional element:

interceptSubmits(document.querySelector('.my-el'), {
  //
  // Leave all these as defaults:
  //
  // dialog: true
  // get: true
  // post: true
  // mailTo: true
  // sameOrigin: true
  // target: true
  // Intercept all submits, even ones that are not same origin
  sameOrigin: false
}, function(e, el) {
  // Change the page state here
});

Thanks Wes Todd for Intercept Link Clicks on which this module is heavily based.