I have an issue with my image view onclick function code. Currently, the code works perfectly but only when I click for the second time. I am not sure what the problem is and would appreciate any help in fixing it. Below is the code snippet:
<script type="text/javascript">
function showimage(z)
{
var modal = document.getElementById('myModal'+z);
var img = document.getElementById('myImg'+z);
var modalImg = document.getElementById('img01'+z);
var captionText = document.getElementById('caption'+z);
img.onclick = function ()
{
modal.style.display = "block";
modalImg.src = this.src;
captionText.innerHTML = this.alt;
}
}
</script>
<span>
<img src="<?php echo $u_pimg; ?>" alt="" class="newsize" id="myImg<?php echo $p_id; ?>" onclick="showimage(<?php echo $p_id; ?>)">
enter code here
</span>