Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

How to hide Bootstrap modal with JavaScript?

  • Difficulty Level : Hard
  • Last Updated : 19 Jun, 2019

This article will tell us how the bootstrap executes when the .modal (modal window) gets closed. At some point of time, the modal window – whenever it gets opened (along with the class modal), it is going to get closed.
As soon the modal has got finished, after being getting hidden from the user, the event will be fired up. The function will get executed and also the below syntax will be triggered, whenever the modal window gets hidden away. By the way, it will call to the caller/user, before disappearing straight away. Also, this is not managed by the user at all. The Bootstrap library is built-in already, and it is going to do the maximum work for you.

The below syntax will be used when the Bootstrap modal is about to be hidden or to hide the Bootstrap modal.
Syntax:

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!

hide.bs.modal

Example: This example shows the usage of hide.bs.modal.




<!DOCTYPE html>
<html>
  
<head>
    <h2 style="color:green">
      GeeksForGeeks
  </h2>
    <h2 style="color:purple">
      Hide Bootstrap Modal
  </h2>
    <meta name="viewport" 
          content="width=device-width, 
                   initial-scale=1">
    <link rel="stylesheet"
          href=
    <script src=
  </script>
    <script src=
  </script>
    <style>
        #myBtn {
            width: 300px;
            padding: 10px;
            font-size: 20px;
            position: absolute;
            margin: 0 auto;
            right: 0;
            left: 0;
            bottom: 50px;
            z-index: 9999;
        }
    </style>
</head>
  
<body style="text-align:center">
    <div class="container">
        <h2>Modal Events - hide.bs.modal</h2>
        <!-- Trigger the modal with a button -->
        <button type="button"
                style="color:brown"
                class="btn btn-info btn-md" 
                id="myBtn">
          Hide Modal
      </button>
        <!-- Modal -->
        <div class="modal fade"
             id="myModal" 
             role="dialog">
            <div class="modal-dialog">
                <!-- Modal content-->
                <div class="modal-content">
                    <div class="modal-header">
                        <button type="button" 
                                class="close" 
                                data-dismiss="modal">
                          ×
                        </button>
                        <h4 class="modal-title">
                          Modal Header: GeeksForGeeks
                        </h4>
                     </div>
                     <div class="modal-body">
                        <p>The <strong>hide.bs.modal</strong
                          is going to hide the modal.</p>
                        <p>If you wish to trigger the modal and 
                          see the modal get hidden, then press
                          the <strong>'HIDE MODAL'</strong> button.
                       </p>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <script>
        $(document).ready(function() {
            $("#myModal").modal("show");
  
            $("#myBtn").click(function() {
                $("#myModal").modal("hide");
            });
  
            $("#myModal").on('hide.bs.modal', function() {
                alert('The modal is about to be hidden.');
            });
        });
    </script>
</body>
  
</html>

Output:
When we load the code:
ngcut



When we click on ‘X’ button:
ngcut

The popup:
ngcut

Result:
ngcut

The working:




My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!