Programmatic control of components and event handling
Control modals with JavaScript methods instead of data attributes.
const modal = new bootstrap.Modal('#myModal');
modal.show(); // Show
modal.hide(); // Hide
modal.toggle(); // Toggle
modal.dispose(); // Dispose
Modals emit events that you can listen to for executing custom code.
| Event | When it Fires |
|---|---|
show.bs.modal |
Before showing |
shown.bs.modal |
After showing |
hide.bs.modal |
Before hiding |
hidden.bs.modal |
After hiding |
const el = document.getElementById('myModal');
el.addEventListener('shown.bs.modal', function(e) {
console.log('Modal shown!');
// Your code here
});
Control carousel programmatically and respond to events.
const carousel = new bootstrap.Carousel('#carousel');
carousel.next(); // Next slide
carousel.prev(); // Previous slide
carousel.pause(); // Pause auto-play
carousel.cycle(); // Resume
carousel.to(2); // Go to slide index 2
Toasts are lightweight notifications that require JavaScript initialization.
const toast = new bootstrap.Toast('#myToast', {
animation: true,
autohide: true,
delay: 3000
});
toast.show();
Offcanvas are sliding panels that can be controlled programmatically.
const offcanvas = new bootstrap.Offcanvas('#myOffcanvas');
offcanvas.show();
offcanvas.hide();
offcanvas.toggle();
new bootstrap.ComponentName(element, options)
show()
,
hide()
, etc.
dispose()
when no longer needed
This offcanvas is controlled programmatically with JavaScript.
You can add any content here: navigation, forms, widgets, etc.