cars cars - 9 months ago 31
CSS Question

How to use jquery .load to open modal

Hi I wrote a code so that I could open a modal once an image was clicked. For example if I clicked the life of pi book cover it would open a modal displaying information about it. But for some reason it doesn't work and I am not sure why. Also note that the life of pi only has a unique id because I was first testing it on just one image. The code is below

enter image description here



$(document).ready(function() {
var $modal = $("#myModal");
$("#lifeofpi").click(function() {
$modal.show();
});
$modal.find('.close').click(function() {
$modal.hide();
});
});

/* The Modal (background) */

.modal {
display: none;
/* Hidden by default */
position: fixed;
/* Stay in place */
z-index: 1;
/* Sit on top */
padding-top: 100px;
/* Location of the box */
left: 0;
top: 0;
width: 100%;
/* Full width */
height: 100%;
/* Full height */
overflow: auto;
/* Enable scroll if needed */
background-color: rgb(0, 0, 0);
/* Fallback color */
background-color: rgba(0, 0, 0, 0.4);
/* Black w/ opacity */
}


/* Modal Content */

.modal-content {
background-color: #fefefe;
margin: auto;
padding: 20px;
border: 1px solid #888;
width: 80%;
}


/* The Close Button */

.close {
color: #aaaaaa;
float: right;
font-size: 28px;
font-weight: bold;
}

.close:hover,
.close:focus {
color: #000;
text-decoration: none;
cursor: pointer;
}

.review-img {
cursor: pointer;
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<section class='images'>
<img class="review-img" id="lifeofpi" src="./images/lifeofpi.jpg"></img>
<img class="review-img" src="./images/kiterunner.jpg"></img>
<img class="review-img" src="./images/starwars.jpg"></img>
<img class="review-img" src="./images/twilight.jpg"></img>
</section>

<!-- The Modal -->
<div id="myModal" class="modal">

<!-- Modal content -->
<div class="modal-content">
<span class="close">&times;</span>
<p>Some text in the Modal..</p>
</div>

</div>




Answer Source

You need to make the modal visible by changing the css attribute display: none with show().

$(function(){
  var $modal = $("#myModal");
  $(".review-img").each(function() {
    var $image= $(this);
    $image.on("click", function() {
      var url = '/bookDetails/' + $image.data("bookid") + '.html';
      // Test:
      url = 'https://httpbin.org/html';
      $modal.find('.modal-content p').html('Loading...').load(url);
      $modal.show();
    });

  });
  $modal.find('.close').click(function() {
    $modal.hide();
  });
});
/* The Modal (background) */

.modal {
  display: none;
  /* Hidden by default */
  position: fixed;
  /* Stay in place */
  z-index: 1;
  /* Sit on top */
  padding-top: 100px;
  /* Location of the box */
  left: 0;
  top: 0;
  width: 100%;
  /* Full width */
  height: 100%;
  /* Full height */
  overflow: auto;
  /* Enable scroll if needed */
  background-color: rgb(0, 0, 0);
  /* Fallback color */
  background-color: rgba(0, 0, 0, 0.4);
  /* Black w/ opacity */
}


/* Modal Content */

.modal-content {
  background-color: #fefefe;
  margin: auto;
  padding: 20px;
  border: 1px solid #888;
  width: 80%;
}


/* The Close Button */

.close {
  color: #aaaaaa;
  float: right;
  font-size: 28px;
  font-weight: bold;
}

.close:hover,
.close:focus {
  color: #000;
  text-decoration: none;
  cursor: pointer;
}

.review-img {
  cursor: pointer;
  height: 100px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<section class='images'>
  <img class="review-img" data-bookid="lifeOfPi" src="https://images-na.ssl-images-amazon.com/images/I/51atapp7YTL._AC_US320_QL65_.jpg" alt="Life of Pi"></img>
  <img class="review-img" data-bookid="kiteRunner" src="https://images-na.ssl-images-amazon.com/images/I/51MtGFNeYjL._AC_US320_QL65_.jpg" alt="Kite Runner"></img>
  <img class="review-img" data-bookid="starWars" src="https://images-na.ssl-images-amazon.com/images/I/51oqkfvEwZL._AC_US320_QL65_.jpg" alt="Star Wars"></img>
  <img class="review-img" data-bookid="twilight" src="https://images-na.ssl-images-amazon.com/images/I/41K99+cInvL._AC_US320_QL65_.jpg" alt="Twilight"></img>
</section>

<!-- The Modal -->
<div id="myModal" class="modal">
  <!-- Modal content -->
  <div class="modal-content">
    <span class="close">&times;</span>
    <p>Book details loading...</p>
  </div>
</div>