Johann Johann - 5 months ago 62
jQuery Question

Show a bootstrap modal if option is selected

I have a form that contains a dropdown and some options:

<select name='type' id='type'>
<option value='suggestions' >Suggestions</option>
<option value='inquiries' >Inquiries</option>
<option value='donations' >Donations</option>
</select>


I also have a Bootstrap modal window:

<!-- Modal -->
<div id="myModal" class="modal fade" role="dialog">
<div class="modal-dialog">

<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">&times;</button>
<h4 class="modal-title">Modal Header</h4>
</div>
<div class="modal-body">
<p>Some text in the modal.</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>

</div>
</div>


I would like to show the Bootstrap modal if the value "donations" is selected.

I have tried doing this:

$("#type").bind("change", function () {
$('#myModal').modal('show')(this.value === 'donations');
}).change();


The modal is opened by default so I know I'm targeting the modal but I would only like to display when the "donations" option is selected.

Any ideas?

Prs Prs
Answer

You try this way..

$("#type").on("change", function () {        
    $modal = $('#myModal');
    if($(this).val() === 'donations'){
        $modal.modal('show');
    }
});

  $("#type").on("change", function () {        
      $modal = $('#myModal');
      if($(this).val() === 'donations'){
        $modal.modal('show');
    }
 });
<script src="https://code.jquery.com/jquery.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" type="text/css" />
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<select name='type' id='type'>
<option value='suggestions' >Suggestions</option>
<option value='inquiries' >Inquiries</option>
<option value='donations' >Donations</option>
</select>
<!-- Modal -->
    <div id="myModal" class="modal fade" role="dialog">
      <div class="modal-dialog">

        <!-- Modal content-->
        <div class="modal-content">
          <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal">&times;</button>
            <h4 class="modal-title">Modal Header</h4>
          </div>
          <div class="modal-body">
            <p>Some text in the modal.</p>
          </div>
          <div class="modal-footer">
            <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
          </div>
        </div>

      </div>
    </div>

Comments