lbramos lbramos -4 years ago 159
Javascript Question

Open modal on page load - RoR and Jquery

I have a link inside a form that opens a modal:

<% if action_name == "new" %>
<div>
<%= link_to 'Open Modal', search_samples_samples_path, {:remote => true, 'data-toggle' => "modal", 'data-target' => '#modal-window'} %>
</div>
<% end %>


What I want to do now is, when the form opens/loads, open the modal directly, without having to click the link.

What's the best way to do this?

Answer Source

Thanks for your suggestions, it worked with:

$(document).ready(function() {  
 document.getElementById('pcrmodal').click();
});

And I added the "id" part to the link element, and hide it also (with display:none):

<%= link_to 'PCR Anterior?', search_samples_samples_path, {:remote => true, 'data-toggle' =>  "modal", 'data-target' => '#modal-window', 'id' => 'pcrmodal', 'style' => 'display:none'}  %>

Thanks again for your feedback.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download