Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I show a bootstrap modal from within a javascript function?

I have a button on my page that when it is pushed it triggers some javascript functions to occur, and one of those javascript functions to open a bootstrap modal, but I can't seem to get it to work. Here is my code... please help.

 //elsewhere on the form is the button that triggers the javascript
  <a href="#" class="btn btn-danger" id="cancelPObtn" data-dismiss="modal">Yes, cancel it</a>

 <!-- CANCEL A PO MODAL -->
 <div class="modal hide fade" id="error-dialog" style="display: none;">
   <div class="modal-header">
        <a class="close" data-dismiss="modal">x</a>
        <h3>Cancel Purchase Order?</h3>
   </div>
   <div class="modal-body">
   </div>
   <div class="modal-footer">
       <a href="#" class="btn btn-danger btn-modal btn-cancel"  data-dismiss="modal">Yes, cancel it</a>
       <a href="#" class="btn" data-dismiss="modal">Nevermind</a>
   </div>
 </div>




      $('.btn-danger').click(function(event) {
          if(some random conditional statement){
             //some stuff happens here
          }
          else{ 
                //Show form validation error modal-- I know this part is triggered but the modal will not show
                $("#error-dialog").modal("show");
          }
      });
like image 671
silvster27 Avatar asked Aug 07 '12 04:08

silvster27


People also ask

How do you call a modal in Javascript?

Trigger the Modal Via data-* AttributesAdd data-toggle="modal" and data-target="#modalID" to any element.

How do I show a Bootstrap modal?

To trigger the modal window, you need to use a button or a link. Then include the two data-* attributes: data-toggle="modal" opens the modal window. data-target="#myModal" points to the id of the modal.

How do I show a Bootstrap modal inside a div?

To show a bootstrap modal inside a div the logic behind the above code is to append the HTML code of the modal as well as the modal-backdrop inside that specific div(blue div whose position is relative) and then making the CSS for modal and modal-backdrop to position:absolute so that the modal and its background gets ...

What is modal dialog in Javascript?

A modal is a dialog box/popup window that is displayed on top of the current page: Open Modal. ×


1 Answers

$("#error-dialog").modal("show");

This is the correct way, your code simply has an error. Go in to Chrome Console and run this command, it'll work.

like image 179
Andy Avatar answered Sep 28 '22 07:09

Andy