0

Im trying to close a Bootstrap 4 pop-up when someone clicks the X icon.

If I remove the code 'data-backdrop="static" it will close the pop-up whenever I click anywhere outside of the modal. However that wont work because every time someone tries to click the next icons for the carousel it will close the pop-up automatically. I would only like the pop-up to close when someone only clicks the X icon.

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

<div id="myModal" class="modal fade in" role="dialog" data-backdrop="static">
  <div class="modal-dialog">
    <button type="button" class="close" data-dismiss="myModal">&times;</button>

    <div id="carouselExampleControls" class="carousel slide" data-ride="carousel">
      <div class="carousel-inner">
        <div class="carousel-item active">
          <img class="d-block w-100" src="../images/placeimg_550_250_grayscale_any.jpg" alt="First slide">
        </div>
        <div class="carousel-item">
          <img class="d-block w-100" src="https://placeimg.com/550/250/any" alt="Second slide">
        </div>
      </div>
      <a class="carousel-control-prev" href="#carouselExampleControls" role="button" data-slide="prev">
        <span class="carousel-control-prev-icon" aria-hidden="true"></span>
        <span class="sr-only">Previous</span>
      </a>
      <a class="carousel-control-next" href="#carouselExampleControls" role="button" data-slide="next">
        <span class="carousel-control-next-icon" aria-hidden="true"></span>
        <span class="sr-only">Next</span>
      </a>
    </div>
  </div>
</div>


<script type="text/javascript">
  $(window).on('load', function() {
    $('#myModal').modal('show');
  });
</script>
vrintle
  • 5,501
  • 2
  • 16
  • 46
Emanuel
  • 79
  • 3
  • 3
  • 13

3 Answers3

1

You should link to the proper CSS and JavaScript files; and for Bootstrap 4 (current release as of writing is 4.3.1):

The value of the data-dismiss attribute of the close button should also be set to modal:

<button type="button" class="close" data-dismiss="modal">&times;</button>

The close button and the carousel should also be placed inside .modal-content — you can use custom CSS to make the modal be transparent.

Working Example

/* Automatically open the modal */
$('#myModal').modal('show');
/* Make the modal be transparent */
#myModal .modal-content {
  background-color: transparent;
  border: 0;
}
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" />

<div id="myModal" class="modal fade" role="dialog" data-backdrop="static">
  <div class="modal-dialog">
    <div class="modal-content">
      <!-- Button to close the modal -->
      <div><button type="button" class="close" data-dismiss="modal">&times;</button></div>

      <!-- Carousel -->
      <div id="carouselExampleControls" class="carousel slide" data-ride="carousel">
        <div class="carousel-inner">
          <div class="carousel-item active">
            <img class="d-block w-100" src="https://placeimg.com/550/250/nature" alt="First slide">
          </div>
          <div class="carousel-item">
            <img class="d-block w-100" src="https://placeimg.com/550/250/any" alt="Second slide">
          </div>
        </div>
        <a class="carousel-control-prev" href="#carouselExampleControls" role="button" data-slide="prev">
          <span class="carousel-control-prev-icon" aria-hidden="true"></span>
          <span class="sr-only">Previous</span>
        </a>
        <a class="carousel-control-next" href="#carouselExampleControls" role="button" data-slide="next">
          <span class="carousel-control-next-icon" aria-hidden="true"></span>
          <span class="sr-only">Next</span>
        </a>
      </div>
    </div>
  </div>
</div>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
Sally CJ
  • 15,362
  • 2
  • 16
  • 34
0

Add this code to your script tag:

$('#myModal').find(".close").click(function() {
      $('#myModal').modal("hide");
});

so your code should be like this:

<script type="text/javascript">
    $(window).on('load',function(){
        $('#myModal').modal('show');
    });
    $('#myModal').find(".close").on("click", function() {
        $('#myModal').modal("hide");
    });
</script>
Hamed Ghasempour
  • 435
  • 3
  • 12
0

When you launch your modal you can pass the options which are given below:

{ keyboard: false, backdrop: 'static'}

which will disable closing the modal by clicking the backdrop, and the escape-button.

or

<div id="modal" class="modal hide fade in" data-keyboard="false" data-backdrop="static">

And on the close icon you can close the model via adding attribute like

data-dismiss="modal" or ng-click="cancel()"

And add functionality on the cancel() method to close the model

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

Have a look for the same here:

Jayoti Parkash
  • 868
  • 11
  • 26