Bootstrap Stateful Buttons

You will discover in this article how to use Bootstrap to make toggle buttons.


Controlling Button States

In the previous section, you learned about Bootstrap button styling, modifications, creating button groups, and toolbars. However, with Bootstrap, there are even more possibilities for buttons, such as controlling button states, creating toggle buttons with checkbox and radio inputs, and more. In the following section, we will explore these features in detail.

Creating Single Toggle Button

To activate toggling on a single button, where the button's state changes from normal to a pushed state and vice versa, simply add the data attribute data-bs-toggle=button.

If you want to pre-select a button in its toggled state, you can manually add the .active class.

<button type="button" class="btn btn-outline-primary" data-bs-toggle="button" autocomplete="off">Toggle button</button>

<button type="button" class="btn btn-outline-primary active" data-bs-toggle="button" autocomplete="off">Active toggle button</button>

<button type="button" class="btn btn-outline-primary" data-bs-toggle="button" autocomplete="off" disabled>Disabled toggle button</button>

Note: The Mozilla Firefox browser retains the state of buttons even across page loads. To prevent this behavior, you can easily disable it by adding the attribute autocomplete=off to either the form containing the buttons or directly to the input or button element.


Creating Checkbox Button Groups

Additionally, you have the option to combine checkboxes to create checkbox-style toggling within button groups. To see how this works, let's explore the following example:

<div class="btn-group">
<input type="checkbox" class="btn-check" name="options" id="check1" autocomplete="off" checked>
<label class="btn btn-outline-primary" for="check1">Checkbox 1</label>
        
<input type="checkbox" class="btn-check" name="options" id="check2" autocomplete="off">
<label class="btn btn-outline-primary" for="check2">Checkbox 2</label>
        
<input type="checkbox" class="btn-check" name="options" id="check3" autocomplete="off" checked>
<label class="btn btn-outline-primary" for="check3">Checkbox 3</label>
</div>

Note: You can also create checkbox-style toggling for button groups by combining checkboxes. However, be cautious when using the .active class for pre-selecting checkboxes or radio buttons in button groups, as it only changes the visual appearance to make them look selected. To actually preselect them, you need to apply the checked attribute on the input element yourself.


Creating Radio Button Groups

Similarly, you can create radio button-style toggling for button groups.

<div class="btn-group">
<input type="radio" class="btn-check" name="options" id="radio1" autocomplete="off">
<label class="btn btn-outline-primary" for="radio1">Radio 1</label>
    
<input type="radio" class="btn-check" name="options" id="radio2" autocomplete="off" checked>
<label class="btn btn-outline-primary" for="radio2">Radio 2</label>
    
<input type="radio" class="btn-check" name="options" id="radio3" autocomplete="off">
<label class="btn btn-outline-primary" for="radio3">Radio 3</label>
</div>

Methods

The following are the standard Bootstrap button methods:

toggle

The first method toggles the push state of the button, changing its appearance to indicate activation. You can also enable auto toggling of a button by using the data-bs-toggle=button attribute.

<script>
$(document).ready(function () {
    $(".btn").click(function () {
        $(this).button("toggle");
    });
});
</script>

FAQ

What is a Bootstrap Stateful Button, and how does it differ from a regular button?

A Bootstrap Stateful Button is a button component that dynamically changes its appearance and behavior based on different states, such as loading, success, or error. Unlike a regular button, a stateful button provides visual feedback to the user during asynchronous operations, making it ideal for scenarios like AJAX requests or time-consuming tasks.

How do you create a basic Bootstrap Stateful Button, and what are its essential attributes?

To create a basic Bootstrap Stateful Button, use the data-bs-loading-text and data-bs-done-text attributes. For example:

<button type="button" class="btn btn-primary" data-bs-loading-text="Loading..." data-bs-done-text="Done!">Click Me</button>

Here, when the button is clicked, it will display "Loading..." and then switch to "Done!" once the action is complete.

Can you explain the role of the data-bs-loading-text attribute in a Bootstrap Stateful Button?

The data-bs-loading-text attribute specifies the text that the Bootstrap Stateful Button displays when it is in the loading state. It is particularly useful to inform users that an action is in progress, providing feedback during asynchronous operations.

How can you programmatically trigger the loading state of a Bootstrap Stateful Button using JavaScript?

You can programmatically trigger the loading state of a Bootstrap Stateful Button by calling the loading method on its associated StatefulButton instance. Here's an example:

<button type="button" class="btn btn-primary" id="myStatefulBtn" data-bs-loading-text="Loading..." data-bs-done-text="Done!">Click Me</button>

<script>
    const statefulBtn = new bootstrap.StatefulButton(document.getElementById('myStatefulBtn'));
    statefulBtn.loading();
</script>

How do you customize the appearance of a Bootstrap Stateful Button, such as changing its color or size?

You can customize the appearance of a Bootstrap Stateful Button by applying Bootstrap utility classes or your custom styles. For example:

<button type="button" class="btn btn-danger btn-lg" data-bs-loading-text="Loading..." data-bs-done-text="Done!">Custom Button</button>

Here, the button will have a red color (btn-danger) and a large size (btn-lg).

In what scenarios would you use the data-bs-timeout attribute in a Bootstrap Stateful Button?

The data-bs-timeout attribute is useful when you want to set a specific timeout for the loading state of a Bootstrap Stateful Button. It defines the duration (in milliseconds) for which the button will remain in the loading state before transitioning to the done state.

<button type="button" class="btn btn-primary" data-bs-loading-text="Loading..." data-bs-done-text="Done!" data-bs-timeout="3000">Click Me</button>

How can you handle a callback function when a Bootstrap Stateful Button enters the done state?

Use the data-bs-done-callback attribute to specify a callback function that will be executed when the Bootstrap Stateful Button transitions to the done state. For example:

<button type="button" class="btn btn-primary" data-bs-loading-text="Loading..." data-bs-done-text="Done!" data-bs-done-callback="myDoneCallback">Click Me</button>

<script>
    function myDoneCallback() {
        // Custom logic to be executed when the button is in the done state
    }
</script>

How can you disable user interaction with a Bootstrap Stateful Button during the loading state?

To disable user interaction with a Bootstrap Stateful Button during the loading state, use the data-bs-disable-on-loading attribute. This prevents users from clicking the button while an action is in progress.

<button type="button" class="btn btn-primary" data-bs-loading-text="Loading..." data-bs-done-text="Done!" data-bs-disable-on-loading>Click Me</button>

How do you use a Bootstrap Stateful Button with AJAX requests?

Incorporating a Bootstrap Stateful Button with AJAX requests involves initializing the button, handling its click event, performing the AJAX operation, and updating the button's state accordingly. Here's an example:

<button type="button" class="btn btn-primary" id="Button1" data-bs-loading-text="Loading..." data-bs-done-text="Done!">Click Me</button>

<script>
    const ajaxBtn = new bootstrap.StatefulButton(document.getElementById('ajaxBtn'));
    ajaxBtn.element.addEventListener('click.bs.button.data-api', function () {
        ajaxBtn.loading();
        // Perform AJAX operation
        setTimeout(() => {
            ajaxBtn.done();
        }, 2000);
    });
</script>

How does the data-bs-loading-duration attribute affect the behavior of a Bootstrap Stateful Button?

The data-bs-loading-duration attribute defines the duration (in milliseconds) for which the Bootstrap Stateful Button remains in the loading state. After this duration, the button transitions to the done state. This attribute allows you to control the loading state duration based on your specific use case.

Can you explain the purpose of the data-bs-loading-spinner attribute in a Bootstrap Stateful Button?

The data-bs-loading-spinner attribute specifies the HTML content (usually an icon or spinner) that appears during the loading state of a Bootstrap Stateful Button. It provides a visual indicator to users that an action is in progress.

<button type="button" class="btn btn-primary" data-bs-loading-text="Loading..." data-bs-done-text="Done!" data-bs-loading-spinner='<span class="spinner-border spinner-border-sm" role="status" aria-hidden="true"></span>'>Click Me</button>

How do you dynamically update the done state text of a Bootstrap Stateful Button using JavaScript?

Dynamically updating the done state text of a Bootstrap Stateful Button can be achieved by manipulating the button's attributes using JavaScript. Here's an example:

<button type="button" class="btn btn-primary" id="Button2" data-bs-loading-text="Loading..." data-bs-done-text="Done!">Click Me</button>

<script>
    const doneTextBtn = new bootstrap.StatefulButton(document.getElementById('dynamicDoneTextBtn'));
    doneTextBtn.element.setAttribute('data-bs-done-text', 'Completed ' + new Date().toLocaleTimeString());
</script>

How do you create a Bootstrap Stateful Button with a loading state background color?

You can set a loading state background color for a Bootstrap Stateful Button using the data-bs-loading-color attribute. This allows you to customize the background color during the loading state:

<button type="button" class="btn btn-primary" data-bs-loading-text="Loading..." data-bs-done-text="Done!" data-bs-loading-color="#f8d7da">Click Me</button>

How can you create a Bootstrap Stateful Button with different loading and done state icons?

Customize the loading and done state icons of a Bootstrap Stateful Button using the data-bs-loading-icon and data-bs-done-icon attributes. Here's an example:

<button type="button" class="btn btn-primary" data-bs-loading-text="Loading..." data-bs-done-text="Done!" data-bs-loading-icon='<i class="bi bi-arrow-clockwise"></i>' data-bs-done-icon='<i class="bi bi-check2"></i>'>Click Me</button>

How do you create a Bootstrap Stateful Button with a dynamic loading state text?

You can dynamically update the loading state text of a Bootstrap Stateful Button using JavaScript. Here's an example:

<button type="button" class="btn btn-primary" id="Button3" data-bs-done-text="Done!">Click Me</button>

<script>
    const statefulBtn = new bootstrap.StatefulButton(document.getElementById('dynamicLoadingBtn'));
    statefulBtn.element.setAttribute('data-bs-loading-text', 'Loading ' + new Date().toLocaleTimeString());
</script>

How do you handle a callback function when a Bootstrap Stateful Button enters the done state?

Use the data-bs-done-callback attribute to specify a callback function that will be executed when the Bootstrap Stateful Button transitions to the done state. Here's an example:

<button type="button" class="btn btn-primary" data-bs-loading-text="Loading..." data-bs-done-text="Done!" data-bs-done-callback="doneCallback">Click Me</button>

<script>
    function doneCallback() {
        // Custom logic to be executed when the button is in the done state
    }
</script>

How can you create a Bootstrap Stateful Button with a custom loading state duration?

Customize the loading state duration of a Bootstrap Stateful Button using the data-bs-loading-duration attribute. This allows you to control how long the button remains in the loading state:

<button type="button" class="btn btn-primary" data-bs-loading-text="Loading..." data-bs-done-text="Done!" data-bs-loading-duration="2000">Click Me</button>

How do you disable user interaction with a Bootstrap Stateful Button during the loading state?

Disable user interaction with a Bootstrap Stateful Button during the loading state by adding the data-bs-disable-on-loading attribute. This prevents users from clicking the button while an action is in progress.

How can you create a Bootstrap Stateful Button with a loading state progress bar?

Create a Bootstrap Stateful Button with a loading state progress bar using the data-bs-loading-progress attribute:

<button type="button" class="btn btn-primary" data-bs-loading-text="Loading..." data-bs-done-text="Done!" data-bs-loading-progress><span class="visually-hidden">Loading...</span></button>

Conclusion

Bootstrap Stateful Buttons offer a dynamic and interactive user experience, providing responsive feedback and allowing for toggleable states. The framework's commitment to user engagement is evident in its Interactive Button States and Toggleable Button States, ensuring that buttons can adapt to user interactions seamlessly.

The concept of Stateful Button Components introduces a level of interactivity, allowing for actions and reactions within the user interface. The use of Bootstrap Toggle Buttons enhances the design, providing an aesthetically pleasing and user-friendly toggle experience. With Actionable Button States and a State-Driven Button Design, Bootstrap empowers developers to create buttons that go beyond static elements, allowing for a multi-state interaction that responds dynamically to user actions.

The inclusion of Active State Buttons and Multi-State Button Interaction further enriches the user experience, providing visual cues and feedback based on the current state of the button. The responsiveness extends to Responsive Stateful Button Design, ensuring a consistent and engaging presentation across different devices.

In addition to stateful buttons, the ability to create a Bootstrap Single Toggle Element and design Toggleable Buttons allows for the development of single switch buttons that enhance user control. Building upon this, the creation of Checkbox Toggle Button Sets and Grouping Checkbox Toggle Buttons provides developers with flexible options for implementing interactive checkbox groups.

For radio buttons, Stylish Radio Button Groups and the process of Forming Radio Button Sets contribute to the aesthetic appeal of user interfaces. The framework's commitment to customization is highlighted in the creation of Customizable Radio Toggle Button Sets and Multi-Choice Radio Toggle Buttons, offering developers the flexibility to tailor radio button styles to specific design requirements.