Boostrap 5 Modal with JavaScript

Ruth Jul 09, 2026

Bootstrap, the popular CSS framework, has always been a go-to for developers seeking to streamline their workflow and create responsive designs. With the release of Bootstrap 5, the new Modal component has gained significant traction due to its improved functionality and JavaScript integration. Let's delve into how you can create and manipulate Bootstrap 5 modals using JavaScript.

Bootstrap 5 Modal Responsive JavaScript Plugin
Bootstrap 5 Modal Responsive JavaScript Plugin

Before we dive in, ensure you have included the Bootstrap 5 CSS and JavaScript files in your project. You can download them from the official Bootstrap website or use a CDN. Once set up, you're ready to create your first modal.

GoSnippets — Free Code Snippets for Bootstrap, Tailwind, React, CSS, JavaScript & more
GoSnippets — Free Code Snippets for Bootstrap, Tailwind, React, CSS, JavaScript & more

Creating a Bootstrap 5 Modal

Bootstrap 5 modals are built using a combination of HTML, CSS, and JavaScript. They are composed of a modal dialog and an optional backdrop. Let's start by creating a basic modal structure.

Bootstrap Modal Vertically Centered
Bootstrap Modal Vertically Centered

Here's a simple example of a Bootstrap 5 modal in HTML:

```html

```

Initializing the Modal with JavaScript

Now that we have our modal structure, let's initialize it using JavaScript. Bootstrap 5 uses the new data-bs- attributes for its components. To initialize the modal, we'll use the following JavaScript:

```javascript const myModal = new bootstrap.Modal(document.getElementById('myModal')); ```

This code initializes the modal with the ID 'myModal'. You can now show or hide the modal using the following methods:

Alert box modal coloridos com boostrap
Alert box modal coloridos com boostrap

```javascript // Show the modal myModal.show(); // Hide the modal myModal.hide(); ```

Manipulating Modal States with Data Attributes

Bootstrap 5 modals also support data attributes for controlling their state. You can use the 'data-bs-toggle' and 'data-bs-target' attributes to trigger modals. Here's an example:

```html ```

In this example, clicking the button will trigger the modal with the ID 'myModal'. The 'data-bs-toggle' attribute sets the trigger type to 'modal', and 'data-bs-target' specifies the modal to be triggered.

Animated bootstrap modal popup
Animated bootstrap modal popup

Manipulating Modals Dynamically

Sometimes, you might want to change the content of your modal dynamically. This can be achieved using JavaScript. Let's say you want to change the modal title and body content:

#CSS Animations Tips and Tricks
#CSS Animations Tips and Tricks
Toggle Fullscreen Mode in Javascript
Toggle Fullscreen Mode in Javascript
The JavaScript Cheat Sheet Every Beginner Wishes They Had
The JavaScript Cheat Sheet Every Beginner Wishes They Had
Difference Between Bootstrap and CSS
Difference Between Bootstrap and CSS
Full-Stack Developer
Full-Stack Developer
Botão css
Botão css
html and css login form
html and css login form
JavaScript Form Events: Handle User Input Like a Pro 📝
JavaScript Form Events: Handle User Input Like a Pro 📝
HTML,CSS JAVASCRIPT cheatsheet
HTML,CSS JAVASCRIPT cheatsheet
Javascript scroll to top | Javascript tutorial
Javascript scroll to top | Javascript tutorial
Javascript window.confirm() method | Javascript tutorial
Javascript window.confirm() method | Javascript tutorial
an image of a computer screen with the text'flexbox basics 2'on it
an image of a computer screen with the text'flexbox basics 2'on it
Javascript Fundamental
Javascript Fundamental
Bootstrap 3 Tutorials - # 9 - Contact Form (in modal)
Bootstrap 3 Tutorials - # 9 - Contact Form (in modal)
JavaScript Cheatsheet for Beginners | Learn JS Fast (One-Page Guide)
JavaScript Cheatsheet for Beginners | Learn JS Fast (One-Page Guide)
an image of a screen with text on it that says, simple like button animation
an image of a screen with text on it that says, simple like button animation
5 Essential JavaScript Array Methods Every Developer Should Master
5 Essential JavaScript Array Methods Every Developer Should Master
Login/Registration Page Design in HTML/CSS/Bootstrap 5
Login/Registration Page Design in HTML/CSS/Bootstrap 5

```javascript const myModal = new bootstrap.Modal(document.getElementById('myModal')); const modalTitle = document.getElementById('exampleModalLabel'); const modalBody = document.getElementById('modalBody'); function changeModalContent(newTitle, newBody) { modalTitle.innerText = newTitle; modalBody.innerText = newBody; myModal.show(); } ```

In this example, the 'changeModalContent' function updates the modal title and body with the provided arguments and then shows the modal.

Handling Modal Events

Bootstrap 5 modals also provide events that you can listen to and handle using JavaScript. Some of the events include 'show.bs.modal', 'shown.bs.modal', 'hide.bs.modal', and 'hidden.bs.modal'. Here's an example of listening to the 'show' event:

```javascript myModal-element.addEventListener('show.bs.modal', event => { // Do something... }); ```

In this example, the code inside the event listener will run whenever the modal is shown.

In conclusion, creating and manipulating Bootstrap 5 modals with JavaScript is a straightforward process. By understanding the HTML structure, JavaScript initialization, and event handling, you can create dynamic and engaging user experiences. Now that you have the knowledge, go ahead and incorporate Bootstrap 5 modals into your projects. Happy coding!