Is this an javascript array?

function show() {
  document.getElementById('id1').style.maxHeight = "600px";
  var images = document.querySelectorAll("#id1 img");
  for(var i = 0; i < images.length; i++)
  {
    images[i].src = images[i].getAttribute('data-src');
  }
}


I think what you meant to do is:


function show() {

     const id1 = document.getElementById("id1");
     const images = id1.querySelectorAll("img");
   
     id1.style.maxHeight = "600px";

     Array.from(images).forEach(function(image){
          image.src = image.getAttribute("data-src");
     });

}
1 Like

thank so mush very handy

Here’s the es6 version:


function show() {

     const id1 = document.getElementById("id1");
     const images = id1.querySelectorAll("img");
   
     id1.style.maxHeight = "600px";

     [...images].forEach((image) => image.src = image.getAttribute("data-src"));

}

thanks alot super useful answer

one more question how can i make this code actually do the contrary instead of show the array now hide the array, I have been trying to do it with something similar to this
function hide() { document.getElementById("id1").style.visibility="hidden", document.getElementById("id1").style.display="none"; }
but dont work

function show() {

     const id1 = document.getElementById("id1");
     const images = id1.querySelectorAll("img");
   
     [...images].forEach((image) => image.style.display = "none");

}