Bootstrap Stateful Buttons
Learn how to create loading buttons, toggle buttons, active buttons and disabled buttons using Bootstrap 5.
Introduction
Stateful buttons change their appearance or behavior when users interact with them. For example, a button can display a loading spinner while saving data, become disabled during processing, or remain selected after being clicked.
Learning Objectives
- Understand stateful buttons.
- Create a loading button with a spinner.
- Create toggle buttons using Bootstrap 5.
- Understand active and disabled states.
- Build interactive button examples.
How Stateful Buttons Work
The user clicks the button.
Bootstrap or JavaScript changes its state.
The button shows loading or selected status.
1. Loading Button
A loading button indicates that an operation is in progress. We can disable the button and display a spinner until the operation finishes.
Program Code
<button id="saveBtn"
class="btn btn-primary"
type="button">
Save Changes
</button>
<script>
document.getElementById("saveBtn").addEventListener("click", function () {
const button = this;
button.disabled = true;
button.innerHTML =
'<span class="spinner-border spinner-border-sm me-2"></span>Saving...';
setTimeout(function () {
button.disabled = false;
button.textContent = "Save Again";
}, 2000);
});
</script>
Program Output
Click the button to see its loading state.
2. Toggle Button
A toggle button switches between selected and unselected
states. Bootstrap 5 provides
data-bs-toggle="button" for this behavior.
Program Code
<button type="button"
class="btn btn-outline-primary"
data-bs-toggle="button"
aria-pressed="false">
Follow
</button>
Program Output
Click Follow to select it. Click again to deselect it.
3. Active and Disabled Buttons
An active button appears selected. A disabled button cannot be clicked by the user.
Program Code
<button type="button"
class="btn btn-success active"
aria-pressed="true">
Active
</button>
<button type="button"
class="btn btn-secondary"
disabled>
Disabled
</button>
Program Output
4. Toggle Button Group
Toggle button groups let users select one or more options. Here, users can select Online, Offline or both training modes.
Program Code
<input type="checkbox"
class="btn-check"
id="onlineCourse"
autocomplete="off">
<label class="btn btn-outline-primary"
for="onlineCourse">
Online
</label>
<input type="checkbox"
class="btn-check"
id="offlineCourse"
autocomplete="off">
<label class="btn btn-outline-primary"
for="offlineCourse">
Offline
</label>
Program Output
Select either option or both options.
Important Classes and Attributes
| Class / Attribute | Purpose |
|---|---|
spinner-border |
Displays a loading spinner. |
disabled |
Prevents user interaction with a button. |
active |
Gives a button a selected appearance. |
data-bs-toggle="button" |
Enables Bootstrap toggle-button behavior. |
btn-check |
Creates a button-style checkbox or radio input. |
aria-pressed |
Communicates a toggle button's pressed state. |
Real-Life Example
On a training institute website, a student clicks Submit Enquiry. The button changes to Submitting... while the enquiry is being processed. Once the server responds, the website displays the appropriate result.
Common Mistakes
- Forgetting to re-enable the button after processing.
- Displaying success before the server confirms it.
- Forgetting to include the Bootstrap JavaScript bundle when using Bootstrap's toggle behavior.
- Reusing the same HTML ID for multiple elements.
Summary
- Stateful buttons respond to user interaction.
- Loading buttons can display a spinner.
- Toggle buttons switch between selected and unselected states.
- The active class gives a button a selected appearance.
- The disabled attribute prevents interaction.