Bootstrap

Bootstrap JS Modal

Bootstrap JS Modals: Create Beautiful Popups with Minimal Code

Modals are one of the most useful UI components — whether you’re displaying alerts, forms, confirmation dialogs, or embedded content. With Bootstrap’s built-in JavaScript Modal component, you can build fully functional popups quickly, without any extra libraries.

In this post, we’ll walk through:

  • ✅ Creating a basic modal

  • Controlling it with JavaScript

  • ⚙️ Custom options & events

  • Real-world use cases


Step 1: Include Bootstrap

Use Bootstrap 5+ via CDN or your build system.

html
<!-- Bootstrap CSS -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">

<!-- Bootstrap Bundle JS (includes Modal plugin) -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>


Step 2: Basic Modal Markup

html
<!-- Trigger Button -->
<button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#exampleModal">
Launch Modal
</button>

<!-- Modal -->
<div class="modal fade" id="exampleModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">

<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Modal Title</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>

<div class="modal-body">
This is your modal content.
</div>

<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>

</div>
</div>
</div>

✅ The modal is triggered by the button via data-bs-toggle="modal" and data-bs-target="#exampleModal".


JavaScript Control (Optional)

You can also control modals programmatically using Bootstrap’s Modal class.

Show / Hide Modal via JS:

javascript
const modalEl = document.getElementById('exampleModal');
const modal = new bootstrap.Modal(modalEl);

// Show the modal
modal.show();

// Hide the modal
modal.hide();

// Toggle (if needed)
modal.toggle();


Modal Events

Bootstrap modals trigger several events you can hook into:

javascript
modalEl.addEventListener('show.bs.modal', () => {
console.log('Modal is about to be shown');
});

modalEl.addEventListener('shown.bs.modal', () => {
console.log('Modal is fully shown');
});

modalEl.addEventListener('hide.bs.modal', () => {
console.log('Modal is about to be hidden');
});

modalEl.addEventListener('hidden.bs.modal', () => {
console.log('Modal is now hidden');
});


⚙️ Common Modal Options

Option Description
backdrop: 'static' Prevents closing by clicking outside
keyboard: false Disables closing via ESC key
focus: false Prevents modal from gaining focus

Example:

javascript
const customModal = new bootstrap.Modal(modalEl, {
backdrop: 'static',
keyboard: false
});

customModal.show();


Real-World Examples

✅ Confirmation Modal:

html
<button class="btn btn-danger" data-bs-toggle="modal" data-bs-target="#deleteModal">Delete</button>

<div class="modal fade" id="deleteModal">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-body">Are you sure you want to delete this item?</div>
<div class="modal-footer">
<button class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
<button class="btn btn-danger" onclick="confirmDelete()">Delete</button>
</div>
</div>
</div>
</div>

✅ Form Inside Modal:

html
<form>
<div class="modal-body">
<input type="text" class="form-control" placeholder="Your name">
</div>
<div class="modal-footer">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</form>

Final Thoughts

Bootstrap’s Modal component is a quick and reliable way to build responsive, accessible, and powerful popups for your UI. With both declarative (HTML) and programmatic (JS) options, it’s easy to integrate and customize.


Useful Links

Leave a Reply

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