3
I have a modal of images but only works with the first one, can anyone help me ? see the modal here: https://jsfiddle.net/pnavtmcx/
3
I have a modal of images but only works with the first one, can anyone help me ? see the modal here: https://jsfiddle.net/pnavtmcx/
8
The problem is that the two images have the same ID and is an attribute that should be unique. One option is to create any attribute and select the elements by querySelector:
Would look like this:
HTML
<img id="myImg1" get-img alt="Texto qualquer" width="300" height="200">
<img id="myImg2" get-img alt="Texto qualquer" width="300" height="200">
Javascript:
var imgs = document.querySelectorAll('[get-img]');
var modalImg = document.getElementById("img01");
var captionText = document.getElementById("caption");
for (var i = 0 ; i < imgs.length ; i++){
imgs[i].onclick = function(){
modal.style.display = "block";
modalImg.src = this.src;
captionText.innerHTML = this.alt;
}
}
Jsfiddle: https://jsfiddle.net/pnavtmcx/6/
4
id is an attribute that must be assigned to only one element, change its code to use the image class as the javascript target.
Classes are used precisely to interact with more than one element in the DOM.
var x = document.getElementsByClassName("example");
Something like this, in X you get all the elements with that class in an array, which starts with index 0.
Browser other questions tagged javascript jquery
You are not signed in. Login or sign up in order to post.
All that remained was to adjust the css: https://jsfiddle.net/kdk9ztyt/
– Ivan Ferrer
Thanks @Ivanferrer, ajustei!
– BrTkCa