Success & Error State Button Template

Give users definitive feedback on their actions with a button that visually reports success or failure.

This template is perfect for form submissions where users need to know instantly if their information was processed correctly or if an error occurred.

Screenshot of buttons displaying success and error feedback states.

Get Source Code Preview

About the Success & Error State Button

After a user clicks a "Submit" or "Save" button, showing only a loading spinner leaves them in suspense. A stateful button resolves this by completing the feedback loop. It transitions from loading to a clear result state, using color and iconography (a green checkmark for success, a red cross for failure) to communicate the outcome instantly. After a brief period, it reverts to its original state, ready for another interaction.

Features

Code Breakdown

The HTML for each button includes the default text and all possible icons (spinner, success, error), with most hidden by default using CSS. JavaScript adds state-specific classes (is-loading, is-success, is-error) to the button based on the outcome of a simulated process. These classes trigger CSS rules that change the button's background color and determine which icon is visible. The script uses two chained setTimeout functions to manage the timing of the state transitions.

Code

Here is the complete HTML, CSS, and JavaScript for the stateful feedback buttons.

View Output