Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bootstrap modal hide is not working

Bootstrap modal hide is not working. Alert comes in else. but my modal is not hidden Added bootply. My issue is the same one.

<button class="button primary" id="buy" data-toggle="modal" data-target=".bs-example-modal-sm" style= "text-decoration:none;" type="button">Review and confirm</button>  <div class="modal-bootstrap fade bs-example-modal-sm" id="myModal" tabindex="-1" role="dialog" aria-labelledby="smallModalLabel" aria-hidden="true">   <div class="modal-bootstrap-dialog modal-sm">     <div class="modal-bootstrap-content">       <div class="modal-bootstrap-body">         -- content ----       </div>     </div>   </div>   <div class="modal-bootstrap-footer">      <button type="submit" class="button primary" data-dismiss="modal">Close</button>      <button type="submit" class="button primary">Save changes</button>   </div> </div>    <script type="text/javascript"> $("#buy").click(function () {    var a = 4;    if (a == 5) {      alert("if");      $('#myModal').modal('show');    }    else {     alert("else");     $('#myModal').modal('hide');    }  }); </script> 

bootply

like image 796
Sam Avatar asked May 15 '14 12:05

Sam


People also ask

How do I hide modals in Bootstrap?

Answer: Use the modal('hide') Method You can simply use the modal('hide') method to hide or close the modal window in Bootstrap using jQuery. Other related Bootstrap's modal methods are modal('show') and modal('toggle') .

What is modal hide?

BootstrapWeb DevelopmentCSS Framework. The . modal(“hide”) method in Bootstrap hides the modal. Hide the Bootstrap modal on the click of a button − $("#button1").

What is data dismiss modal?

modal-header class is used to define the style for the header of the modal. The <button> inside the header has a data-dismiss="modal" attribute which closes the modal if you click on it. The . close class styles the close button, and the . modal-title class styles the header with a proper line-height.

How do you close a modal in HTML?

To close a modal, add the w3-button class to an element together with an onclick attribute that points to the id of the modal (id01). You can also close it by clicking outside of the modal (see example below). Tip: &times; is the preferred HTML entity for close icons, rather than the letter "x".


1 Answers

You are using both modal toggling methods: via Javascript and via data attributes. So your click is firing the modal show as your data attributes set, and your Javascript does not affect this trigger.

Just remove the data attributes and go with the Javascript method:

<button class="button primary" id="buy" style="text-decoration:none;" type="button">Review and confirm</button>  <div class="modal-bootstrap fade bs-example-modal-sm" id="myModal" tabindex="-1" role="dialog" aria-labelledby="smallModalLabel" aria-hidden="true">     <!-- modal contents --> </div>  <script type="text/javascript"> $("#buy").click(function () {     var a = 4;     if (a == 5) {         alert("if");         $('#myModal').modal('show');     } else {         alert("else");         $('#myModal').modal('hide');     } }); </script> 
like image 177
albertedevigo Avatar answered Oct 08 '22 22:10

albertedevigo