Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bootstrap modal - close modal when "call to action" button is clicked

I have a external link inside my modal, and I want the modal to hide after the user has clicked on the link. How do I do that?

Here is my code:

<div class="modal hide fade" id="modalwindow">   <div class="modal-header">     <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>     <h3>Title</h3>   </div>   <div class="modal-body">     <p>You need to do a search on google.com for that.</p>   </div>   <div class="modal-footer">     <a id="closemodal" href="https://www.google.com" class="btn btn-primary" target="_blank">Launch google.com</a>   </div> </div>  <script type="text/javascript">     $('#closemodal').modal('hide'); </script> 
like image 665
Tomas Jacobsen Avatar asked Oct 16 '12 12:10

Tomas Jacobsen


People also ask

How do you close a modal by clicking on a button?

To close a modal using vue. js you can use the click event i.e. @click to trigger change in modal visibility. So, whenever the close button is pressed the @click method will trigger the function associated with the action ( here, hiding the modal ).

How do I turn off close modal when I click outside?

On Options chapter, in the page you linked, you can see the backdrop option. Passing this option with value 'static' will prevent closing the modal. As @PedroVagner pointed on comments, you also can pass {keyboard: false} to prevent closing the modal by pressing Esc .

How do you automatically close a modal?

Inside the Open event handler of the jQuery UI Dialog Modal Popup box, using the JavaScript setTimeout function and the jQuery UI Dialog “close” command, the jQuery UI Dialog Modal Popup box is set to automatically close after delay of 5 seconds (some seconds). This dialog will automatically close in 5 seconds.

How do I open a modal when I click a button?

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.


2 Answers

You need to bind the modal hide call to the onclick event.

Assuming you are using jQuery you can do that with:

$('#closemodal').click(function() {     $('#modalwindow').modal('hide'); }); 

Also make sure the click event is bound after the document has finished loading:

$(function() {    // Place the above code inside this block }); enter code here 
like image 192
jsalonen Avatar answered Oct 11 '22 05:10

jsalonen


Remove your script, and change the HTML:

<a id="closemodal" href="https://www.google.com" class="btn btn-primary close" data-dismiss="modal" target="_blank">Launch google.com</a> 

EDIT: Please note that currently this will not work as this functionality does not yet exist in bootstrap. See issue here.

like image 25
Praveen Kumar Purushothaman Avatar answered Oct 11 '22 05:10

Praveen Kumar Purushothaman