The idea is the visitor clicks on a thumbnail and a modal opens with the bigger image. I am trying to get the file name onClick (when the visitor clicks on the image) and then I would like to insert it into a link of the now open modal to show the bigger image. So far I just have created a modal for every image, but this is getting ridiculous long.
This is what I tried now so far:
This is the image with the tag to open the modal. The image is in the thumbnail folder and I would like to save the name inst01.jpg into a variable.
<a href="#" data-toggle="modal" data-target="#imageModal"><img class="img-fluid" src="/images/thumbs/inst01.jpg" onclick="getName()"></a>
This is the modal. The original image is in the gallery folder and I would like to insert the saved file name inst01.jpg into the link to open the original image:
<div class="modal fade" tabindex="-1" role="dialog" id="imageModal">
<div class="modal-dialog modal-dialog-centered" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Image 1</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<div class="container">
<img src="images/gallery/inst01sm.jpg" alt="" class="img-fluid">
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
I tried to google for a javascript function and I came up with this one:
function getName() {
var fullPath = document.getElementById("img1").src;
var filename = fullPath.replace(/^.*[\\\/]/, '');
// or, try this,
// var filename = fullPath.split("/").pop();
document.getElementById("result").value = filename;
}
But I have no idea where I have to create the filename variable, nor do I know how to create function which would insert the filename into another link when the modal opens.
I hope it is clear what I want to do. Thanks for reading so far and helping out.