Phipsen
Phipsen

Reputation: 169

Close modal dialog when opening link in new browser tab/window

I'd like to close the modal bootstrap dialog when the user hits "Open ticket", which in turn opens a new browser window/tab. Here is my button:

<%= link_to content_tag(:i, '', class: "fa fa-ticket") + "Open ticket",
      "https://link.to/another/company/url",
      target: '_blank',
      class: 'btn btn-default' 
%>

This works fine, but when returning to the calling page, the modal is still open.

I tried to add data: { dismiss: 'modal' }, but then the link isn't being opened.

Thanks.

Upvotes: 2

Views: 2173

Answers (1)

Shehary
Shehary

Reputation: 9992

Can suggest you solution,

if you trying like this data: { dismiss: 'modal' } you should try like this

<%= link_to content_tag(:i, '', class: "fa fa-ticket") + "Open ticket",
      "https://link.to/another/company/url",
      target: '_blank',
      class: 'btn btn-default',
      data-dismiss: 'modal'
%>

Alternate you can do with jQuery, as you have class selector here class: 'btn btn-default' in button to open the link

<%= link_to content_tag(:i, '', class: "fa fa-ticket") + "Open ticket",
      "https://link.to/another/company/url",
      target: '_blank',
      class: 'btn btn-default' 
%>

Add a customer selector e.g close-modal, like class: 'btn btn-default close-modal'

and bind the jQuery with selector close-modal to close the modal when button clicked.

$(document).ready(function() {
    $('.close-modal').click(function(){
        $('#modalid').modal('hide');  //change #modalid to your bootstrap modal id
    });
});

Upvotes: 2

Related Questions