PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, May 5, 2022

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

 May 05, 2022     image, javascript, string, url     No comments   

Issue

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?


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 - 1];


Answered By - Jacopo Bonomi
Answer Checked By - Robin (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing