Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bootstrap Modal Hide function doesn't work

I used Bootstrap modal dialog with OK/Cancel button. I expect some work to be done after OK is clicked and then close the modal dialog. So I used JS to handle OK click.

HTML:

      <div class="modal fade" id="RequestModal" tabindex="-1">
        <div class="modal-dialog">
          <div class="modal-content">
            <div class="modal-header">
              <h5 class="modal-title" id="RequestModalLabel">Confirm to submit below request?</h5>
            </div>
            <div class="modal-body">
              ...
            </div>
            <div class="modal-footer">
              <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
              <button type="button" class="btn btn-primary" id="btn_close" onclick="f_test()">OK</button>
            </div>
          </div>
        </div>
      </div>

JS

function f_test () {
      var myModal = new bootstrap.Modal(document.getElementById('RequestModal'));
      myModal.hide();
}

The hide function doesn't work. I searched the stackoverflow and others but didn't get good answer.

like image 957
user3449977 Avatar asked Oct 27 '25 10:10

user3449977


1 Answers

You are very close. Create the object of the modal dialog first and then use it to hide on the f_test() method as shown below,

 var myModal = new bootstrap.Modal(document.getElementById('RequestModal'));
 myModal.show();

function f_test () {

   myModal.hide();
}

JSFiddle: https://jsfiddle.net/78n9k65L/

like image 143
JGV Avatar answered Oct 29 '25 02:10

JGV



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!