Size of the Javascript Image

Asked

Viewed 1,484 times

-1

Good evening guys, I’m having a problem in javascript which is as follows:

I need to dynamically create div in a for and put an image in these Ivs, I was able to do this with the following code:

for(y = 0;y<z;y++){
         passarImagem[y] = document.createElement("div");

         var testando = document.createElement("img");
         testando = "<img src=exemmplo.png>";
         testando.width = 500;
          testando.height = 500;

         passarImagem[y].innerHTML = testando.src;
     }

I need to scale these images to a specific size, I put as example 500, but this way I did it does not work, I wonder if there is a way to change the size of this image.

1 answer

3

Here is an example using appendChild(), that is nothing more than to create a "son" for a "father", where each element <img> is a son and the main div the father:

for(y = 0; y < 10; y++){
  principal = document.getElementById("principal");
  
  var imagem = document.createElement("img");
  
  imagem.src = "https://i.imgur.com/F543MoZ.png";
  imagem.width = 30;
  imagem.height = 30;

  principal.appendChild(imagem);
}
<div id="principal"></div>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.