Two Ways to Use Bootstrap JavaScript
| Way | How | Best For |
|---|---|---|
| Data attributes | Write data-bs-* in HTML only | Simple pages |
| JavaScript API | Create and control components in code | Dynamic apps |
Data Attributes
Most of the time you do not write any JavaScript. Bootstrap watches the page for data attributes and does the work.
Options can also be set as data attributes by adding the option name: data-bs-backdrop="static".
Creating Components with JavaScript
getOrCreateInstance
Creating the same component twice can cause bugs. Use getOrCreateInstance to reuse an existing one:
Common Methods
| Component | Methods |
|---|---|
| Modal, Offcanvas | show(), hide(), toggle(), dispose() |
| Collapse | show(), hide(), toggle() |
| Carousel | next(), prev(), pause(), cycle(), to(index) |
| Tab | show() |
| Toast | show(), hide() |
| Tooltip, Popover | show(), hide(), enable(), disable() |
Events
Components fire events when they open or close. Event names look like show.bs.modal, shown.bs.modal, hide.bs.modal and hidden.bs.modal.
show fires at the start, right when the open command is called. shown fires after the animation has finished. The same applies to hide and hidden.
Example: Open a Modal After 3 Seconds
Conclusion
You can build many things using only data attributes. When you need more control, the JavaScript API lets you open, close and react to every component.

