Skip to content
Related Articles

Related Articles

How to hide Bootstrap modal with JavaScript?

Improve Article
Save Article
  • Difficulty Level : Hard
  • Last Updated : 19 Jun, 2019
Improve Article
Save Article

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.

Example: This example shows the usage of

<!DOCTYPE html>
    <h2 style="color:green">
    <h2 style="color:purple">
      Hide Bootstrap Modal
    <meta name="viewport" 
    <link rel="stylesheet"
    <script src=
    <script src=
        #myBtn {
            width: 300px;
            padding: 10px;
            font-size: 20px;
            position: absolute;
            margin: 0 auto;
            right: 0;
            left: 0;
            bottom: 50px;
            z-index: 9999;
<body style="text-align:center">
    <div class="container">
        <h2>Modal Events -</h2>
        <!-- Trigger the modal with a button -->
        <button type="button"
                class="btn btn-info btn-md" 
          Hide Modal
        <!-- Modal -->
        <div class="modal fade"
            <div class="modal-dialog">
                <!-- Modal content-->
                <div class="modal-content">
                    <div class="modal-header">
                        <button type="button" 
                        <h4 class="modal-title">
                          Modal Header: GeeksForGeeks
                     <div class="modal-body">
                        <p>The <strong></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.
        $(document).ready(function() {
            $("#myBtn").click(function() {
            $("#myModal").on('', function() {
                alert('The modal is about to be hidden.');

When we load the code:

When we click on ‘X’ button:

The popup:


The working:

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!