Reputation: 95
I currently have it so that onClick the modal opens ,but it does not display the image..
Here's the code
HTML
<div class="item">
<img src="Imgs/10.jpg" alt="Hello welcome to the picture thingy" onclick="showImage('Imgs/10.jpg');" style="width:100%;" id="myImg" />
<p class="xtratekst">
othertext
</p></div><div id="myModal" class="modal">
<!-- The Close Button -->
<span class="close" onclick="document.getElementById('myModal').style.display='none'">×</span>
!-- Modal Content (The Image) -->
<img class="modal-content" id="image">
<!-- Modal Caption (Image Text) -->
<div id="caption"></div>
</div>
JavaScript
// Get the modal
var modal = document.getElementById('myModal');
// Get the image and insert it inside the modal - use its "alt" text as a caption
var img = document.getElementById('myImg');
var modalImg = document.getElementById("image");
var captionText = document.getElementById("caption");
function showImage(){
modal.style.display = "block";
modalImg.src = this.src;
captionText.innerHTML = this.alt;
}
// Get the <span> element that closes the modal
var span = document.getElementsByClassName("close")[0];
// When the user clicks on <span> (x), close the modal
span.onclick = function() {
modal.style.display = "none";
}
Upvotes: 0
Views: 1473
Reputation: 267
You showImage
function should take a parameter:
function showImage(image){
Then set the modal image src to that parameter:
modalImg.src = image
Upvotes: 1