Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to hide Bootstrap modal with javascript?

I've read the posts here, the Bootstrap site, and Googled like mad - but can't find what I'm sure is an easy answer...

I have a Bootstrap modal that I open from a link_to helper like this:

<%= link_to "New Contact", new_contact_path, {remote: true, 'data-toggle' => 'modal', 'data-target' => "#myModal",  class: "btn btn-primary"} %> 

In my ContactsController.create action, I have code that creates Contact then passes off to create.js.erb. In create.js.erb, I have some error handling code (a mix of ruby and javascript). If everything goes well, I want to close the modal.

This is where I'm having trouble. I can't seem to dismiss the modal when all goes well.

I've tried $('#myModal').modal('hide'); and this has no effect. I've also tried $('#myModal').hide(); which causes the modal to dismiss but leaves the backdrop.

Any guidance on how to close the modal and/or dismiss the backdrop from within create.js.erb?

Edit

Here's the markup for myModal:

<div class="modal hide" id="myModal" >   <div class="modal-header">     <a class="close" data-dismiss="modal">×</a>     <h3>Add Contact</h3>     <div id="errors_notification">     </div>   </div>   <div class="modal-body">     <%= form_for :contact, url: contacts_path, remote: true do |f| %>         <%= f.text_field :first_name, placeholder: "first name" %>       <%= f.text_field :last_name, placeholder: "last name" %>       <br>       <%= f.submit "Save", name: 'save', class: "btn btn-primary" %>       <a class="close btn" data-dismiss="modal">Cancel</a>     <% end %>   </div>   <div class="modal-footer">   </div> </div> 
like image 469
jvillian Avatar asked May 05 '12 21:05

jvillian


People also ask

How do I stop Bootstrap modal popup?

There are few ways to close modal in Bootstrap: click on a backdrop, close icon, or close button. You can also use JavaScript hide method. Click the button to launch the modal. Then click on the backdrop, close icon or close button to close the modal.

How do I turn off modal?

We can close the modal pop-up in the following ways:modal('hide'); //Using modal pop-up Id.

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

With the modal open in the browser window, use the browser's console to try

$('#myModal').modal('hide'); 

If it works (and the modal closes) then you know that your close Javascript is not being sent from the server to the browser correctly.

If it doesn't work then you need to investigate further on the client what is happening. Eg make sure that there aren't two elements with the same id. Eg does it work the first time after page load but not the second time?

Browser's console: firebug for firefox, the debugging console for Chrome or Safari, etc.

like image 83
Larry K Avatar answered Oct 08 '22 10:10

Larry K