Home > Back-end >  How to change a string to a URL and import it as an image?
How to change a string to a URL and import it as an image?

Time:04-10

I am thinking of something like this:

let img_1 = "./img/image.png";
let player = 1;
document.getElementById("player_img").src = "img_"   player;

then it can display ./img/image.png on the webpage

but the code above does not work, what is the right code?

CodePudding user response:

Probably the best approach is use an array.

Like this

let img_ = ["./img/image.png","./img/image.png","./img/image.png"];
let player = 1;
document.getElementById("player_img").src = img_[player];
  • Related