Ver Mensaje Individual
  #1 (permalink)  
Antiguo 22/04/2020, 10:14
Avatar de kahlito
kahlito
Colaborador
 
Fecha de Ingreso: marzo-2003
Ubicación: En el Estrecho y el mar
Mensajes: 2.936
Antigüedad: 21 años, 1 mes
Puntos: 65
Detectar valor document.getElementById() dentro de un array

Hola.

Tengo una galería de imágenes donde intento que cuando el usuario haga click en cada una de ellas se muestre esa misma imagen en grande. Lo logro hacer con su correspondiente "id" y con javascript.

Ejemplo:

Código HTML:
Ver original
  1. <li><img id="azul" src="img/azul.png" ></li>
  2. <li><img id="amarillo" src="img/amarillo.png" ></li>
  3. <li><img id="verde" src="img/verde.png" ></li>
  4. <li><img id="rojo" src="img/verde.png" ></li>

Código Javascript:
Ver original
  1. <script>
  2. // Get the modal //
  3. var modal = document.getElementById("myModal");
  4.  
  5.  
  6. var img1 = document.getElementById("azul");
  7. var img2 = document.getElementById("amarillo");
  8. var img3 = document.getElementById("verde");
  9. var img3 = document.getElementById("rojo");
  10.  
  11. var modalImg = document.getElementById("img-modal-grande");
  12. var captionText = document.getElementById("caption");
  13.  
  14. //Click en imágenes a mostrar
  15. img1.onclick = function(){  
  16.   modal.style.display = "flex";
  17.   modalImg.src = this.src;
  18.   captionText.innerHTML = this.alt;
  19. }
  20.  
  21. img2.onclick = function(){
  22.   modal.style.display = "flex";
  23.   modalImg.src = this.src;
  24.   captionText.innerHTML = this.alt;
  25. }
  26. img3.onclick = function(){  
  27.   modal.style.display = "flex";
  28.   modalImg.src = this.src;
  29.   captionText.innerHTML = this.alt;
  30. }
  31. img4.onclick = function(){  
  32.   modal.style.display = "flex";
  33.   modalImg.src = this.src;
  34.   captionText.innerHTML = this.alt;
  35. }
  36.  
  37. // Get the <span> element that closes the modal
  38. var span = document.getElementsByClassName("close")[0];
  39.  
  40. // When the user clicks on <span> (x), close the modal
  41. span.onclick = function() {
  42.   modal.style.display = "none";
  43. }
  44.  
  45. </script>

Hasta aquí bien, pero claro si son 20 imágenes e intento hacerlo en un array al intentar coger el valor dentro de document.getElementById() no lo reconoce

Código Javascript:
Ver original
  1. <script>
  2.  
  3. var modal = document.getElementById("myModal");
  4.  
  5. //Coge el valor para luego devolverlo al hacer onclick
  6.  
  7.  
  8. var myArray = ['azul', 'amarillo', 'verde', 'rojo' ];
  9.  
  10. for(i=0;i<myArray.length;i++){
  11.    
  12.     var img = document.getElementById(myArray[i]);
  13.  
  14.     //Prueba añadiendo de nuevo el valor a id, donde aquí si lo reconoce pero al hacer clien en la imagen luego no hace la fución.
  15.     //var img = document.getElementById(myArray[i]).id = myArray[i];   
  16.  
  17. }
  18.  
  19. var modalImg = document.getElementById("img-modal-grande");
  20. var captionText = document.getElementById("caption");
  21.  
  22. //Click en imágenes a mostrar
  23. img.onclick = function(){  
  24.   modal.style.display = "flex";
  25.   modalImg.src = this.src;
  26.   captionText.innerHTML = this.alt;
  27. }
  28.  
  29.  
  30. // Get the <span> element that closes the modal
  31. var span = document.getElementsByClassName("close")[0];
  32.  
  33. // When the user clicks on <span> (x), close the modal
  34. span.onclick = function() {
  35.   modal.style.display = "none";
  36. }
  37.  
  38. </script>

¿Cómo podría hacer que reconozca el valor del id dentro del array para luego devolverlo o llamarlo al hacer click en cada imagen?

Saludos.