Forgive me if my issue seems insignificant to those well-versed in JS. I've developed an image carousel and am working on linking a thumbnail to a full-size image, so that when a user clicks on the thumbnail, the larger image appears in another section of the page.
The code snippet within the window.onload handler is as follows:
for (var i = 0; i < numImages; ++i) {
var image = images[i],
frame = document.createElement('div');
frame.className = 'pictureFrame';
/* some styling skipped */
carousel.insertBefore(frame, image);
frame.appendChild(image);
} /* for */
Initially, I tried adding an "onclick" function at the end of the loop:
frame.onclick= function () {
var largeImage = document.getElementById('largeImage');
largeImage.style.display = 'block';
largeImage.style.width=200+"px";
largeImage.style.height=200+"px";
var url=largeImage.getAttribute('src');
document.getElementById('slides').style.display = 'block';
document.getElementById("slides").innerHTML="<img src='url' />";
}
However, this approach only works with hardcoded ids like 'largeImage'. Ideally, I want to pass image.src as a parameter, but using
(frame.onclick= function (image.src))
doesn't work.
My next idea was to encapsulate the logic of fetching image.src into a separate function and assign it to frame.onclick= myFunction;
.
But then I stumbled upon this example:
<input type="button" value="Click me" id="elem">
<script>
elem.onclick = function(event) {
// show event type, element and coordinates of the click
alert(event.type + " at " + event.currentTarget);
alert("Coordinates: " + event.clientX + ":" + event.clientY);
};
</script>
This got me wondering why the handler in this example can accept a parameter.
What is the correct way to link an image to the onclick event? Or is there a more efficient method to turn a thumbnail into a clickable link?