Jamgreen
Jamgreen

Reputation: 11039

Show/hide modal in Meteor

How can I show a bootstrap modal in Meteor?

From http://meteorpedia.com/read/Modals I can read that the Meteor-way is to use template logic to show/hide the modal.

I have tried adding the modal in my template with

<div class="modal fade">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
          <span class="sr-only">Close</span>
        </button>
        <h4 class="modal-title">Modal title</h4>
      </div>
      <div class="modal-body">
        <p>One fine body&hellip;</p>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
        <button type="button" class="btn btn-primary">Save changes</button>
      </div>
    </div><!-- /.modal-content -->
  </div><!-- /.modal-dialog -->
</div><!-- /.modal -->

and I have a variable showModal which determines whether the modal should be shown or not. But I don't know how to programmatically show/hide the modal.

Upvotes: 3

Views: 6805

Answers (2)

Tushar
Tushar

Reputation: 87203

Add an id to the modal

<div id="myModal" class="modal fade">

And use modal() to open the modal

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

To close the modal

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

Docs


If you want to open the modal in Meteor way

In JS

Session.set('showModal', true); // Show modal
Session.set('showModal', false); // Hide modal

In Template

{{#if showModal}}
    <div class="modal fade"> <!-- Use Display block to show the modal by default -->
    ...

{{/if}}

Upvotes: 6

Nate
Nate

Reputation: 1875

Make life easy on yourself and use a bootstrap meteor-modal package. I use this one and it works great meteor modal.

Upvotes: 2

Related Questions