Bootstrap 5 Modal toggle() Method Last Updated : 31 Jul, 2024 Comments Improve Suggest changes Like Article Like Report The Bootstrap 5 Modal toggle() method is used to directly toggle a modal i.e. show or hide. This method shows the result before the show or hide event occurs.Syntax:modal.toggle()Return Value: The user receives a direct response from this method before the modal is ever displayed or hidden.Example 1: In this article, we used the toggle() method of the Bootstrap 5 Modal component to toggle its visibility. Here the modal's visibility is toggled when we click the button and after 3 seconds it toggles its visibility again. 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 Modal toggle() Method</title> <link href= "https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet"> <script src= "https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.min.js"> </script> </head> <body> <div class="container"> <div> <h1 class="text-success"> GeeksforGeeks </h1> <h5>Bootstrap 5 Modal toggle() Method</h5> </div> <button class="btn btn-success" onclick="toggleModal1()"> Toggle Modal </button> <div id="gfg" class="modal" tabindex="-1"> <div class="modal-dialog"> <div class="modal-content"> <div class="modal-header"> <h5 class="modal-title"> GeeksforGeeks </h5> </div> <div class="modal-body"> <p>GeeksforGeeks is a computer science portal for geeks. </p> </div> </div> </div> </div> </div> <script> var modal1 = new bootstrap.Modal(document.getElementById('gfg')); function toggleModal1() { // Toggle Modal modal1.toggle(); // Toggle again after 3 seconds setTimeout(() => { modal1.toggle(); }, 3000) } </script> </body> </html> Output:Example 2: This is another example illustrating the use of the toggle() method of the Modal component with buttons. 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 Modal toggle() Method</title> <link href= "https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet"> <script src= "https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.min.js"> </script> </head> <body> <div class="container"> <div> <h1 class="text-success"> GeeksforGeeks </h1> <h5>Bootstrap 5 Modal toggle() Method </h5> </div> <button class="btn btn-success" onclick="toggleModal1()"> Toggle Modal </button> <div id="gfg" class="modal" tabindex="-1"> <div class="modal-dialog"> <div class="modal-content"> <div class="modal-header"> <h5 class="modal-title"> GeeksforGeeks </h5> <button type="button" class="btn-Decline" data-bs-dismiss="modal" aria-label="Decline"> </button> </div> <div class="modal-body"> <p>We have updated our Privacy Policy. Accept it to continue . </p> </div> <div class="modal-footer"> <button type="button" class="btn btn-danger" data-bs-dismiss="modal"> Decline </button> <button type="button" class="btn btn-success"> Accept </button> </div> </div> </div> </div> </div> <script> var modal1 = new bootstrap.Modal(document.getElementById('gfg')); function toggleModal1() { // Toggle Modal modal1.toggle(); } </script> </body> </html> Output:Reference: https://getbootstrap.com/docs/5.0/components/modal/#toggle Comment More infoAdvertise with us Next Article Bootstrap 5 Modal Remove Animation P prashantrathore1159 Follow Improve Article Tags : Web Technologies Bootstrap Bootstrap-5 Similar Reads 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 Bootstrap 5 Modal Remove Animation Bootstrap 5 Modal is used to show dialogues to the frontend user using bootstrap's JavaScript library. The Modal used Bootstrap's fade animation when it is opened and closed. The animations can be removed by removing the fade class from the modal component or by manually setting the transition and t 3 min read Bootstrap 5 Modal Dynamic Heights A website can have dialogues added for lightboxes, user notifications, or entirely unique content by using Bootstrap modals. Dynamic heights can be implemented which can be really useful in a modal that changes the content shown in it. This can be achieved using the modal.handleupdate() function. Sy 3 min read Bootstrap 5 Modal Optional Sizes Bootstrap 5 Modal Optional sizes have different optional sizes, to be placed along with the modal-dialog class. This is used to create different sizes modals.Bootstrap 5 Modal Optional sizes Classes:modal-sm: This class is used for creating a small modal having a width of 300px.modal-lg: This class 2 min read Bootstrap 5 Fullscreen Modal Bootstrap 5 Fullscreen Modal is used to create a modal that completely covers the entire screen of the user.Bootstrap 5 Fullscreen Modal Used CLasses:modal-fullscreen: This class is used to create Full Screen Modalmodal-fullscreen-*-down: This class is used to create a full-screen modal based upon b 2 min read Bootstrap 5 Modal Usage Bootstrap 5 Modal plugin is used to create a customized webpage where we can add dialog boxes for user notifications and lightboxes. Bootstrap 5 modal can hide the content on demand with the help of JavaScript or via data attributes. Default scrolling behavior can also be overridden by modal and it 4 min read Bootstrap 5 Modal Via data attributes Bootstrap 5 Modal Via data attributes can activate a modal, by setting data-bs-toggle="modal" on an element, like a button. We can also toggle a specific modal by using a data-bs-target="#foo" or href="#foo". Bootstrap 5 Modal Via data attributes: data-bs-toggle="modal": To tell the button, that we 2 min read Like