Bootstrap

Bootstrap Js Button

⚡ Bootstrap JS Buttons: Adding Power to Your UI with Just a Few Lines

Buttons are a core part of any user interface — and when you’re using Bootstrap, they come with built-in JavaScript behavior that can enhance your app without writing much JS yourself.

In this post, we’ll explore how to create and control interactive Bootstrap buttons using Bootstrap’s JavaScript features — including toggles, button groups, and loading states.


Basic Bootstrap Button

html
<button type="button" class="btn btn-primary">Click Me</button>

This gives you a styled button — but no JS yet. Let’s add interactivity.


1. Toggle Button (Using JS)

Bootstrap allows buttons to toggle between active/inactive states without custom JavaScript.

✅ Example: Toggle Button

html
<button type="button" class="btn btn-outline-primary" data-bs-toggle="button" aria-pressed="false">
Toggle Me
</button>

What it does:

  • Adds/removes the .active class on click

  • Updates the aria-pressed attribute for accessibility

  • Requires Bootstrap JS to be included!

✅ Enable Bootstrap JS:

Make sure you’ve included Bootstrap’s JS in your HTML:

html
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>

2. Button Groups with Toggle Behavior

html
<div class="btn-group" role="group" aria-label="Toggle group">
<input type="radio" class="btn-check" name="options" id="option1" autocomplete="off" checked>
<label class="btn btn-outline-primary" for="option1">One</label>

<input type="radio" class="btn-check" name="options" id="option2" autocomplete="off">
<label class="btn btn-outline-primary" for="option2">Two</label>

<input type="radio" class="btn-check" name="options" id="option3" autocomplete="off">
<label class="btn btn-outline-primary" for="option3">Three</label>
</div>

Notes:

  • Uses radio inputs and labels styled as buttons

  • Ensures only one button is active at a time


3. Controlling Button State with JavaScript

You can also control a button’s active state using vanilla JS + Bootstrap’s Button methods:

Example:

html
<button type="button" class="btn btn-primary" id="myBtn">Click Me</button>
javascript
const btn = document.getElementById('myBtn');
btn.addEventListener('click', function () {
btn.classList.toggle('active');
});

Or use Bootstrap’s JS API (if you’re using Bootstrap 5+):

javascript
import { Button } from 'bootstrap';

const myButton = document.querySelector('#myBtn');
const bsButton = new Button(myButton);

bsButton.toggle(); // Manually toggle state


4. Simulate Loading States

Bootstrap doesn’t provide built-in loaders on buttons, but you can simulate it easily:

html
<button class="btn btn-primary" id="loadingBtn">Load</button>
javascript
const btn = document.getElementById('loadingBtn');
btn.addEventListener('click', function () {
btn.innerHTML = 'Loading...';
btn.disabled = true;

setTimeout(() => {
btn.innerHTML = 'Done!';
btn.disabled = false;
}, 2000);
});


Final Thoughts

Bootstrap’s JavaScript-enhanced buttons make it easy to create toggleable, grouped, or dynamically updated UI elements with almost no custom JS. For production-grade features (like spinners, loaders, and modals triggered by buttons), Bootstrap provides a clean, modular way to build them.


Resources:

Leave a Reply

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