Close modal when click away

1

I have a modal that when clicking outside it would like it to close. For issues with position issues, 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 did not work.

    
asked by anonymous 18.07.2018 / 15:43

1 answer

0

I would do so depending on the Bootstrap version.

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