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.

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
- Full Feedback Cycle: The button cycles through four states: default, loading, result (success or error), and back to default.
- Clear Iconography: Uses embedded SVGs for the checkmark and cross icons for sharp, scalable visuals.
- Accessible Feedback: The template includes an
aria-live
region, which audibly announces the result to screen reader users, ensuring the feedback is not just visual. - Prevents Duplicate Submissions: The button is disabled throughout the loading and result phases.
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.