Close modal by clicking outside

Asked

Viewed 876 times

1

I have a modal that when I click out of it I would like it to close. For reasons of position problems, I’m using

 data-backdrop=false

My modal:

<div class="bd-example">
    <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal" data-whatever="@mdo">Open modal for @mdo</button>
    <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal" data-whatever="@fat">Open modal for @fat</button>
    <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal" data-whatever="@getbootstrap">Open modal for @getbootstrap</button>
    <div class="modal fade" data-backdrop="false" style="background-color: rgba(0, 0, 0, 0.5);" style="background-color: rgba(0, 0, 0, 0.5)" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
      <div class="modal-dialog" role="document">
          <div class="modal-content">
              <div class="modal-header">
                  <h4 class="modal-title" id="exampleModalLabel">New message</h4>
                  <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                      <span aria-hidden="true">&times;</span>
                  </button>
              </div>
              <div class="modal-body">
                  <form>
                      <div class="md-form">
                          <input type="text" class="form-control" id="recipient-name">
                      </div>
                      <div class="md-form">
                          <textarea type="text" id="message-text" class="form-control md-textarea" rows="3" ></textarea>
                      </div>
                  </form>
              </div>
              <div class="modal-footer">
                  <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                  <button type="button" class="btn btn-primary">Send message</button>
              </div>
          </div>
      </div>
    </div>
</div>

I tried something like:

<script>
$('.modal').click(function(event){
    $('.modal').modal('hide');
});
</script>

but it didn’t work.

  • Would Bootsrap be this modal? Which version?

  • is from mdb, last version

  • tries to take the data-backdrop="false" or set to true

  • can not, I have a parent element with fixed position that can not change, if I set true the gray screen is in front of the modal...

1 answer

0


I would do so depending on the version of Bootstrap.

$(document).click(function (e) {
    if ($(e.target).is('#openModal')) {
        $('#exampleModal').fadeOut(500);
    }
});

Browser other questions tagged

You are not signed in. Login or sign up in order to post.