Skip to content
Related Articles

Related Articles

Improve Article

How to get the same behavior with confirm and bootstrap modal?

  • Last Updated : 07 Aug, 2019
Geek Week

Bootstrap modal:
In simple words, the Modal component is a dialog box/popup window that is displayed on top of the current page, once the trigger button is clicked. However, clicking on the modal’s backdrop automatically closes the modal. Also, it must be kept in mind that Bootstrap doesn’t support nested modals as they create bad UI experience for the user. Therefore, only one modal window is supported at a time.

Example:




<html>
<script src=
  </script>
<script src=
  </script>
<script src=
  </script>
<link rel="stylesheet"
      href=
<link rel="stylesheet" 
      href=
<script src=
        integrity=
"sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" 
        crossorigin="anonymous">
  </script>
<script src=
        integrity=
"sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" 
        crossorigin="anonymous">
  </script>
  
<body>
    <center>
        <h1 style="color:green">GeeksforGeeks</h1>
  
        <!-- Button trigger modal -->
        <button type="button" 
                id="launchid" 
                class="btn btn-primary" 
                data-toggle="modal" 
                data-target="#exampleModal">
            Launch demo modal
        </button>
  
        <!-- Modal -->
        <div class="modal fade" 
             id="exampleModal"
             tabindex="-1"
             role="dialog" 
             aria-labelledby="exampleModalLabel" 
             aria-hidden="true">
            
            <div class="modal-dialog" 
                 role="document">
                <div class="modal-content">
                    <div class="modal-header">
  
                        <h5 class="modal-title" 
                            id="exampleModalLabel">
                          Modal title
                       </h5>
                        <button type="button" 
                                class="close"
                                data-dismiss="modal"
                                aria-label="Close">
                            
                            <span aria-hidden="true">
                              ×
                          </span>
                        </button>
                    </div>
                    <div class="modal-body">
                        Woohoo, you're reading this text in a modal!
                    </div>
                    <div class="modal-footer">
                        <button type="button" 
                                id="closeid"
                                class="btn btn-secondary" 
                                data-dismiss="modal">
                          Close
                      </button>
                        <button type="button" 
                                id="saveid" 
                                class="btn btn-primary">
                          Save changes
                      </button>
                    </div>
                </div>
            </div>
        </div>
  
        <div class="alert" role="alert" id="result"></div>
    </center>
    <script>
        var modalConfirm = function(callback) {
  
            $("#launchid").on("click", function() {
                $("exampleModal").modal('show');
            });
  
            $("#saveid").on("click", function() {
                callback(true);
                $("#exampleModal").modal('hide');
            });
  
            $("#closeid").on("click", function() {
                callback(false);
                $("#exampleModal").modal('hide');
            });
        };
  
        modalConfirm(function(confirm) {
            if (confirm) {
                $("#result").html("Changes Saved");
            } else {
  
                $("#result").html("Not Saved");
            }
        });
    </script>
</body>
  
</html>

Output:

Hey geek! The constant emerging technologies in the world of web development always keeps the excitement for this subject through the roof. But before you tackle the big projects, we suggest you start by learning the basics. Kickstart your web development journey by learning JS concepts with our JavaScript Course. Now at it’s lowest price ever!




My Personal Notes arrow_drop_up
Recommended Articles
Page :