Load Spinner On Submit Button While Submitting Form | SpinOnSubmitJS

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:

  1. Create a submit button element in your HTML form with a unique ID.
  2. In your JavaScript file, import the createSpinnerButton function from the SpinOnSubmitJS library.
  3. 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.
  4. 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

loading-spinner-form-submit-js

Official Website(): Click Here

This superior jQuery/javascript plugin is developed by . For extra advanced usage, please go to the official website.

Related Posts

drag-drop-file-5x5-jq-uploader

Responsive Drag and Drop File Uploader/Download Plugin | 5x5_jq_uploader

5x5_jq_uploader plugin can be used to instantly create a drop file area and file queue with a little bit of preparation. Bootstrap is used for responsive planning…

Bootstrap-Dark-Mode

Stylesheet For Implementing Dark Mode with Bootstrap

Bootstrap Dark Mode provides a style sheet and two texts that allow you to implement Dark Mode on your website. Initially loaded based on user preferences, can…

responsive-navigation-menu

Multi-purpose Navigation Menu for Javascript Library | jQuery Corenav

coreNavigation is a multipurpose navigation menu for a jquery based javascript library, comes with more style, and is easy to integrate. 11 Default Menu 2 Responsive Navigation…

Simple-Multi-Select-Dropdown-Pure-Vanilla-Javascript

Simple Multi-Select Dropdown Pure Vanilla Javascript | multiSelect.js

MultiSelect.js is a simple, clean, and progressive JavaScript library designed for easy integration with any type of project or system. The design was heavily influenced by the…

Confetti-Falling-Animation-Effect-party

Confetti Falling Animation Effect In JavaScript | party.js

Party.js is a JavaScript library to brighten user site experience with visual effects! Celebrate success with dom confetti! The library is written in TypeScript and compiled into…

how-to-create-popup-in-html-with-css

How To Create A Popup in HTML with CSS

How to create popup in html with css – Popup without JavaScript is an elegant pop-up panel component with an animated scale, written in CSS. Have you…

Leave a Reply

Your email address will not be published. Required fields are marked *