Upon opening my page, I am attempting to create a modal dialog that displays an image. Strangely, I keep receiving an error stating the image is undefined when trying to display it without using the onclick function. I am working with a modified version of a script from W3Schools. Any suggestions on how to fix this issue would be greatly appreciated! Below is the code snippet:
HTML:
<img id="myImg" src="http://192.185.4.115/~hitclubar/images/prueba.png" alt="" width="300" height="200" style="display:none;">
<div id="myModal" class="modal">
<a href="http://facebook.com/Hitclubar">Click Evento Facebook</a>
<span class="close">×</span>
<img class="modal-content" id="img01">
<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("img01");
var captionText = document.getElementById("caption");
$(function(){
modal.style.display = "block";
modalImg.src = document.getElementById('myImg');
modalImg.alt = "HELLO";
captionText.innerHTML = "HELLO";
});
// 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";
}
CSS:
#myImg {
border-radius: 5px;
cursor: pointer;
transition: 0.3s;
}
#myImg:hover {opacity: 0.7;}
.modal {
display: none;
position: fixed;
z-index: 1;
padding-top: 100px;
left: 0;
top: 0;
width: 100%;
height: 100%;
overflow: auto;
background-color: rgb(0,0,0);
background-color: rgba(0,0,0,0.9);
}
.modal-content {
margin: auto;
display: block;
width: 80%;
max-width: 700px;
}
#caption {
margin: auto;
display: block;
width: 80%;
max-width: 700px;
text-align: center;
color: #ccc;
padding: 10px 0;
height: 170px;
}
.modal-content, #caption {
-webkit-animation-name: zoom;
-webkit-animation-duration: 0.6s;
animation-name: zoom;
animation-duration: 0.6s;
}
/* Additional CSS styles are included in the provided code */