Skip to content

Bootstrap JavaScript Plugins

Bootstrap's interactive components—modals, dropdowns, tooltips, and more—are powered by a small set of vanilla JavaScript plugins. This lesson covers both the data attribute and programmatic APIs.

Bootstrap JavaScript Plugins Overview

Most Bootstrap components can be controlled declaratively through data-bs-* attributes without writing any JavaScript, which is how the modal, dropdown, collapse, and offcanvas examples in earlier lessons worked. Every plugin also exposes a matching JavaScript class, like bootstrap.Modal or bootstrap.Tooltip, for programmatic control.

Each plugin class provides methods like show(), hide(), and toggle(), and dispatches custom events—such as shown.bs.modal or hide.bs.dropdown—that you can listen for to run your own code at the right moment, like fetching data right before a modal opens.

Concept Description Common Usage
Data attribute API data-bs-toggle, data-bs-target, etc. Declarative, no-JavaScript component control
JavaScript API new bootstrap.Modal(el), .show(), .hide() Programmatic control from custom scripts
Plugin events shown.bs.modal, hide.bs.dropdown, etc. Reacting to component state changes
getInstance() / getOrCreateInstance() Access an existing plugin instance Avoiding duplicate initialization
Popper.js dependency Required for dropdowns, tooltips, popovers Positioning floating UI elements correctly

Bootstrap JavaScript Plugins Example

<button id="openModalBtn" class="btn btn-primary">Open via JavaScript</button>
<div class="modal" id="infoModal" tabindex="-1">
  <div class="modal-dialog"><div class="modal-content">
    <div class="modal-body">Loaded dynamically before opening.</div>
  </div></div>
</div>

<script>
  const modalEl = document.getElementById("infoModal");
  const modal = new bootstrap.Modal(modalEl);

  document.getElementById("openModalBtn").addEventListener("click", () => {
    modal.show();
  });

  modalEl.addEventListener("shown.bs.modal", () => {
    console.log("Modal is now fully visible");
  });
</script>

Instead of using data-bs-toggle="modal", this example creates a bootstrap.Modal instance directly in JavaScript and calls its show() method on a custom button click, which is useful when you need extra logic before opening. The shown.bs.modal event listener demonstrates reacting to the moment the modal finishes its opening transition.

Top 10 Bootstrap JavaScript Plugins Examples

These are the JavaScript API patterns you'll use to control Bootstrap components programmatically.

# Example Syntax
1 Create a modal instance new bootstrap.Modal(document.getElementById('myModal'));
2 Show a modal via JS modal.show();
3 Hide a modal via JS modal.hide();
4 Toggle a dropdown via JS new bootstrap.Dropdown(el).toggle();
5 Initialize a tooltip new bootstrap.Tooltip(el, { title: 'Info' });
6 Listen for a shown event el.addEventListener('shown.bs.modal', callback);
7 Listen for a hide event el.addEventListener('hide.bs.offcanvas', callback);
8 Get an existing instance bootstrap.Modal.getInstance(el);
9 Get or create an instance safely bootstrap.Modal.getOrCreateInstance(el);
10 Dispose a plugin instance modal.dispose();

Best Practices

  • Use the data attribute API for simple, static interactions and the JavaScript API when you need custom logic.
  • Always dispose of plugin instances when their element is removed from the DOM, especially in single-page apps.
  • Use getOrCreateInstance instead of always calling new, to avoid creating duplicate instances accidentally.
  • Listen for plugin events (shown, hidden, etc.) instead of using arbitrary setTimeout delays to sequence code.
  • Initialize plugins like tooltips explicitly with JavaScript, since they require opt-in initialization unlike modals or dropdowns.
  • Keep Popper.js available by using the bundled JS file whenever tooltips, popovers, or dropdowns are used.
  • Read the specific plugin's documentation for its full events and options list before writing custom integration code.

Common Mistakes

  • Assuming every component works automatically; tooltips and popovers require explicit initialization.
  • Creating a new plugin instance every time a function runs, leading to duplicated behavior or memory leaks.
  • Forgetting to dispose of instances in single-page apps, causing stale event listeners to accumulate.
  • Not loading the bundled JS (with Popper) when using dropdowns, tooltips, or popovers.
  • Using generic click listeners to reimplement behavior Bootstrap's plugin API already provides.
  • Ignoring plugin events and instead polling or guessing when a component finished animating.

Key Takeaways

  • Bootstrap plugins can be controlled through data attributes or a programmatic JavaScript API.
  • Each plugin exposes methods like show(), hide(), and toggle() plus lifecycle events.
  • getOrCreateInstance avoids duplicate plugin instances when working with dynamic content.
  • Some components, like tooltips, require explicit JavaScript initialization to work at all.
  • Popper.js, included in the bundled JS file, powers accurate positioning for floating UI elements.

Pro Tip

When integrating Bootstrap components into a JavaScript framework like React or Vue, always create the plugin instance in a lifecycle hook (like useEffect or onMounted) and call dispose() in its cleanup function to avoid leaking instances across re-renders.