SpinOnSubmitJS
is a lightweight JavaScript library that provides an easy way to add a spinner to a submit button when performing asynchronous actions, such as form submissions. It simplifies the process of indicating the loading status and disabling the button while waiting for the action to complete.
This creates a button that, when clicked, collects data from a form, displays an animated yellow button to the right of the button text, and pops up an alert with the form data. If any of the fields are empty, the function will be rejected with an error, and the error callback will alert the user. If the ‘hideLabelWhileLoading
‘ option is set to true, the button label will be hidden while the spinner is displayed.
Using SpinOnSubmitJS in your project is easy. Follow these steps:
- Create a submit button element in your HTML form with a unique ID.
- In your JavaScript file, import the
createSpinnerButton
function from the SpinOnSubmitJS library. - Call
createSpinnerButton
, passing in the following arguments:- The ID of your submit button
- The ID of your form
- A callback function represents the asynchronous action to be performed when the button is clicked. This callback receives the form data as its first argument, removing the need to manually gather the data.
- An error callback function. This function will be called if your main callback throws an error or rejects a promise, allowing you to handle errors gracefully.
- An options object that can contain the following properties:
spinnerColor
: This sets the color of the default spinner.position
: This can be either ‘left’ or ‘right’, and sets the position of the spinner in relation to the button text.hideLabelWhileLoading
: This can be either true or false and determines whether or not the button’s text is hidden while the spinner is displayed.
- Remember, the options object is an optional parameter. If you want the spinner with default configurations, you don’t need to pass this argument.
Must Read: Simple Full-Screen Loading Spinner Library | H5Loading.js
How to make use of it:
Install SpinOnSubmitJS via npm:
npm install spinonsubmitjs
Use createSpinnerButton:
createSpinnerButton( 'submitBtn', 'myForm', function(data) { return new Promise(function(resolve, reject) { setTimeout(function() { var firstName = data.firstName; var lastName = data.lastName; if (firstName === '' || lastName === '') { reject('All fields must be filled!'); } else { alert("Submitted!\nFirst Name: " + firstName + "\nLast Name: " + lastName); resolve(); } }, 2000); }); }, function(error) { alert(error); console.error(error); }, 'white', // Spinner color 'right', // Spinner position true // hideLabelWhileLoading );
See Demo And Download

Official Website(): Click Here
This superior jQuery/javascript plugin is developed by . For extra advanced usage, please go to the official website.