If you use a bundler (Webpack, Rollup…), you can use /js/dist/*.js files which are UMD ready.

Using Bootstrap as a module

We provide a version of Bootstrap built as ESM (bootstrap.esm.js and bootstrap.esm.min.js) which allows you to use Bootstrap as a module in your browser, if your .

Incompatible plugins

Due to browser limitations, some of our plugins, namely Dropdown, Tooltip and Popover plugins, cannot be used in a <script> tag with module type because they depend on Popper. For more information about the issue see .

Dependencies

Some plugins and CSS components depend on other plugins. If you include plugins individually, make sure to check for these dependencies in the docs.

Our dropdowns, popovers and tooltips also depend on .

Bootstrap 5 is designed to be used without jQuery, but it’s still possible to use our components with jQuery. If Bootstrap detects jQuery in the window object it’ll add all of our components in jQuery’s plugin system; this means you’ll be able to do $('[data-bs-toggle="tooltip"]').tooltip() to enable tooltips. The same goes for our other components.

Data attributes

Nearly all Bootstrap plugins can be enabled and configured through HTML alone with data attributes (our preferred way of using JavaScript functionality). Be sure to only use one set of data attributes on a single element (e.g., you cannot trigger a tooltip and modal from the same button.)

Selectors

Currently to query DOM elements we use the native methods querySelector and querySelectorAll for performance reasons, so you have to use valid selectors. If you use special selectors, for example: collapse:Example be sure to escape them.

Events

All infinitive events provide preventDefault() functionality. This provides the ability to stop the execution of an action before it starts. Returning false from an event handler will also automatically call preventDefault().

  1. var myModal = document.getElementById('myModal')
  2. myModal.addEventListener('show.bs.modal', function (event) {
  3. if (!data) {
  4. return event.preventDefault() // stops modal from being shown
  5. }
  6. })

Bootstrap will detect jQuery if jQuery is present in the window object and there is no data-bs-no-jquery attribute set on <body>. If jQuery is found, Bootstrap will emit events thanks to jQuery’s event system. So if you want to listen to Bootstrap’s events, you’ll have to use the jQuery methods (.on, .one) instead of addEventListener.

  1. $('#myTab a').on('shown.bs.tab', function () {
  2. // do something...
  3. })

Programmatic API

All constructors accept an optional options object or nothing (which initiates a plugin with its default behavior):

  1. var myModalEl = document.getElementById('myModal')
  2. var modal = new bootstrap.Modal(myModalEl) // initialized with defaults

If you’d like to get a particular plugin instance, each plugin exposes a getInstance method. In order to retrieve it directly from an element, do this: bootstrap.Popover.getInstance(myPopoverEl).

You can also use a CSS selector as the first argument instead of a DOM element to initialize the plugin. Currently the element for the plugin is found by the querySelector method since our plugins support a single element only.

All programmatic API methods are asynchronous and return to the caller once the transition is started but before it ends.

In order to execute an action once the transition is complete, you can listen to the corresponding event.

  1. var myCollapseEl = document.getElementById('#myCollapse')
  2. myCollapseEl.addEventListener('shown.bs.collapse', function (event) {
  3. // Action to execute once the collapsible area is expanded
  4. })

In addition a method call on a transitioning component will be ignored.

  1. var myCarouselEl = document.getElementById('myCarousel')
  2. var carousel = bootstrap.Carousel.getInstance(myCarouselEl) // Retrieve a Carousel instance
  3. myCarouselEl.addEventListener('slid.bs.carousel', function (event) {
  4. carousel.to('2') // Will slide to the slide 2 as soon as the transition to slide 1 is finished
  5. })
  6. carousel.to('1') // Will start sliding to the slide 1 and returns to the caller
  7. carousel.to('2') // !! Will be ignored, as the transition to the slide 1 is not finished !!
  1. // changes default for the modal plugin's `keyboard` option to false
  2. bootstrap.Modal.Default.keyboard = false

No conflict (only if you use jQuery)

Sometimes it is necessary to use Bootstrap plugins with other UI frameworks. In these circumstances, namespace collisions can occasionally occur. If this happens, you may call .noConflict on the plugin you wish to revert the value of.

Version numbers

The version of each of Bootstrap’s plugins can be accessed via the VERSION property of the plugin’s constructor. For example, for the tooltip plugin:

  1. bootstrap.Tooltip.VERSION // => "5.0.0"

Bootstrap’s plugins don’t fall back particularly gracefully when JavaScript is disabled. If you care about the user experience in this case, use <noscript> to explain the situation (and how to re-enable JavaScript) to your users, and/or add your own custom fallbacks.

Third-party libraries

Bootstrap does not officially support third-party JavaScript libraries like Prototype or jQuery UI. Despite .noConflict and namespaced events, there may be compatibility problems that you need to fix on your own.

Sanitizer

Tooltips and Popovers use our built-in sanitizer to sanitize options which accept HTML.

The default allowList value is the following:

  1. var ARIA_ATTRIBUTE_PATTERN = /^aria-[\w-]*$/i
  2. var DefaultAllowlist = {
  3. // Global attributes allowed on any supplied element below.
  4. '*': ['class', 'dir', 'id', 'lang', 'role', ARIA_ATTRIBUTE_PATTERN],
  5. a: ['target', 'href', 'title', 'rel'],
  6. area: [],
  7. b: [],
  8. br: [],
  9. code: [],
  10. div: [],
  11. em: [],
  12. hr: [],
  13. h2: [],
  14. h3: [],
  15. h4: [],
  16. h5: [],
  17. h6: [],
  18. i: [],
  19. img: ['src', 'srcset', 'alt', 'title', 'width', 'height'],
  20. li: [],
  21. ol: [],
  22. p: [],
  23. pre: [],
  24. s: [],
  25. small: [],
  26. span: [],
  27. sub: [],
  28. sup: [],
  29. strong: [],
  30. u: [],
  31. ul: []
  32. }

If you want to add new values to this default allowList you can do the following:

  1. var myDefaultAllowList = bootstrap.Tooltip.Default.allowList
  2. // To allow table elements
  3. myDefaultAllowList.table = []
  4. // To allow td elements and data-bs-option attributes on td elements
  5. myDefaultAllowList.td = ['data-bs-option']
  6. // You can push your custom regex to validate your attributes.
  7. // Be careful about your regular expressions being too lax
  8. myDefaultAllowList['*'].push(myCustomRegex)

If you want to bypass our sanitizer because you prefer to use a dedicated library, for example , you should do the following: