Bootstrap 5 Varying Modal Content
Last Updated :
26 Jul, 2024
Bootstrap 5 Modal Content can be varied by passing the value which needed to be accessed in the modal to a data-bs-* (where * can be anything you like) attribute of the triggering button. The value passes to the modal can be accessed using JavaScript and can be used in the modal content.
Bootstrap 5 Modal Content Classes: There are no specific classes for varying modal content. We just use a data-bs-* attribute to pass the variable value.
Bootstrap 5 Varying modal content Attribute:
- data-bs-*: It is used to vary the contents of the modal depending on which button was clicked.
The * can be replaceable by target and toggle.
Syntax:
<div class="modal" id="mymodal">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h3 class="modal-title">...</h3>
</div>
<div class="modal-body">
...
</div>
</div>
</div>
</div>
Example 1: In this example, we pass the value for the header of the modal so when the modal is opened using different buttons it has different headings.
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible"
content="IE=edge">
<meta name="viewport" content=
"width=device-width, initial-scale=1.0">
<title>Bootstrap 5 - GeeksforGeeks</title>
<!-- Bootstrap CSS -->
<link rel="stylesheet" href=
"https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" />
<!-- Bootstrap Javascript -->
<script src=
"https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.min.js">
</script>
</head>
<body>
<div class="container">
<div class="my-4">
<h1 class="text-success">GeeksforGeeks</h1>
<strong>Bootstrap 5 Varying Modal Content</strong>
</div>
<button type="button" class="btn btn-success"
data-bs-toggle="modal" data-bs-target="#mymodal"
data-bs-heading="About GeeksforGeeks">
Open The Modal
</button>
<a class="btn btn-secondary" data-bs-toggle="modal"
data-bs-target="#mymodal"
data-bs-heading="The Starting of GeeksforGeeks">
Open Modal with a Different Title
</a>
<div class="modal fade" id="mymodal"
data-bs-backdrop="static">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
Title
</div>
<div class="modal-body">
<p>
GeeksforGeeks is a computer science portal for
geeks. It was started in 2009 by Sandeep Jain.
Today it is considered to be one of the best
resources for the students preparing for job
in computer science domain.
</p>
</div>
<div class="modal-footer">
<button type="button"
class="btn btn-danger"
data-bs-dismiss="modal">
Close
</button>
</div>
</div>
</div>
</div>
</div>
<script>
const myModal = document.querySelector('#mymodal');
myModal.addEventListener('show.bs.modal', function (event) {
// Get the reference of the triggering button
// and retrieve the value from the attribute
const button = event.relatedTarget;
const heading = button.getAttribute('data-bs-heading');
// Set the value for the heading
const title = myModal.querySelector('.modal-header').textContent =
heading;
});
</script>
</body>
</html>
Output:
Example 2: We can also use more than one data-bs-* attributes to pass more than one varying content to the modal. The example below illustrates the same. We are passing the modal heading, content, and footer using the attributes on the triggering button.
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible"
content="IE=edge">
<meta name="viewport" content=
"width=device-width, initial-scale=1.0">
<title>Bootstrap 5 - GeeksforGeeks</title>
<!-- Bootstrap CSS -->
<link rel="stylesheet" href=
"https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" />
<!-- Bootstrap Javascript -->
<script src=
"https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.min.js">
</script>
</head>
<body>
<div class="container">
<div class="my-4">
<h1 class="text-success">GeeksforGeeks</h1>
<strong>Bootstrap 5 Varying Modal Content</strong>
</div>
<button
class="btn btn-success"
data-bs-toggle="modal"
data-bs-target="#mymodal"
data-bs-heading="GeeksforGeeks Practice Portal"
data-bs-footercontent="Visit the Practice Portal for more details"
data-bs-modalcontent="Using GFG's Practice portal you can
practice on you problem solving skills.">
Open Modal for Practice Portal
</button>
<button
class="btn btn-warning"
data-bs-toggle="modal"
data-bs-target="#mymodal"
data-bs-heading="GeeksforGeeks Courses"
data-bs-footercontent="Visit the Courses for more details"
data-bs-modalcontent="There are lot of free as well as
paid courses offered by GeeksforGeeks which help
you to improve your skills.">
Open Modal for Courses
</button>
<button
class="btn btn-info"
data-bs-toggle="modal"
data-bs-target="#mymodal"
data-bs-heading="GeeksforGeeks Job Portal"
data-bs-footercontent="Visit the Job Portal for more details"
data-bs-modalcontent="GeeksforGeeks Job Portal helps
you finding a good high paying job if you have skills.">
Open Modal for Job Portal
</button>
<div class="modal" id="mymodal">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h3 class="modal-title">Placeholder Title</h3>
</div>
<div class="modal-body">
Placeholder Content
</div>
<div class="modal-footer text-center">
Placeholder Footer
</div>
</div>
</div>
</div>
</div>
<script>
const myModal = document.querySelector('#mymodal');
myModal.addEventListener('show.bs.modal', function(event) {
// Get the reference of the triggering button
const button = event.relatedTarget;
// Get the data for inserting into modal
const heading = button.getAttribute('data-bs-heading');
const modalcontent = button.getAttribute('data-bs-modalcontent');
const footercontent = button.getAttribute('data-bs-footercontent');
// Set the value in the modal
myModal.querySelector('.modal-title').textContent = heading;
myModal.querySelector('.modal-body').textContent = modalcontent;
myModal.querySelector('.modal-footer').textContent = footercontent;
});
</script>
</body>
</html>
Output:
Reference: https://getbootstrap.com/docs/5.2/components/modal/#varying-modal-content
Similar Reads
Bootstrap 5 Modal Components A website can have dialogues added for lightboxes, user notifications, or entirely unique content by using Bootstrap modals. A modal is made up of components like the dialog, content, header, title, body, and footer. A modal can be used to implement a lot of utilities like showing a message or poppi
4 min read
Bootstrap 5 Modal Live demo Bootstrap 5 Modal live demo demonstrates the toggle of a working modal by clicking a button that opens a modal. The modal content slides down but fades in from the top after the toggle action. Different components are assembled together to create a Modal. Modals can be triggered using the data attri
3 min read
Bootstrap 5 Modal Static backdrop Bootstrap 5 Modal Static backdrop facilitates the modal that will not be going to be closed while clicking outside of the modal, by setting the backdrop to static. Modal Static backdrop Classes: There are no specific classes used in Modal Static backdrop. Although, we can create the modal using Moda
2 min read
Bootstrap 5 Modal Scrolling long content Bootstrap 5 Modal Scrolling long content is used when modal body content becomes very large then it becomes scrollable. Modals can either be scrolled independently of the page or we can scroll the modal body. We will learn about them using examplesSyntax:<div class="modal-dialog modal-dialog-scro
2 min read
Bootstrap 5 Modal Vertically Centered Bootstrap 5 Modal Vertically Centered is used to make the modal centered aligned. Modals are positioned over everything else in the document and the scroll gets removed from the <body> so that modal content scrolls instead. Using bootstrap modal classes we can make the modal body align vertica
3 min read
Bootstrap 5 Modal Tooltips and Popovers Bootstrap 5 provides a robust way to enhance user interfaces through modals, tooltips, and popovers. These components help in displaying additional information to users without cluttering the main content. Tooltips offer brief descriptions, while popovers can contain more detailed information. Both
4 min read
Bootstrap 5 Modal Using the Grid Bootstrap 5 Modal Using the grid can add content which can be anything from text to images to anything we want. We can also create layouts inside a modal too, Grid System being one of the main tools for creating responsive layout modals. So we can add different settings and variations of the Grid Sy
3 min read
Bootstrap 5 Varying Modal Content Bootstrap 5 Modal Content can be varied by passing the value which needed to be accessed in the modal to a data-bs-* (where * can be anything you like) attribute of the triggering button. The value passes to the modal can be accessed using JavaScript and can be used in the modal content. Bootstrap 5
4 min read
Bootstrap 5 Toggle Between Modals Bootstrap 5 Toggle Between Modals is used to switch between modals, this does not mean there can be multiple opened modals, only one modal at a time can be opened, by using this feature we can trigger the other modal that will replace the current modal content with the new ones.Bootstrap 5 Toggle be
3 min read
Bootstrap 5 Modal Change Animation Bootstrap 5 Modal Change animation facilitates several variables that determine the animation transformation state of the modal, along with setting the zoom-in & zoom-out animation for the modal. The Modal opens with a fade-in animation when it is triggered using a button, although, this animati
4 min read