Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to change a string to a URL and import it as an image?

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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];
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading