You can add a variable to the JavaScript image tag by concatenating the variable with the image source using the '+' operator. Here's an example:
let myVariable = "myImage.jpg";
let myImage = document.createElement('img');
myImage.src = "images/" + myVariable;
In this code snippet, the variable myVariable
contains the name of the image, and the myImage
variable is created using the createElement
method. The src
attribute of the image tag is then set to the concatenation of the images/
directory and myVariable
. This will result in the image URL being set to images/myImage.jpg
.
Asked: 2023-06-18 04:29:28 +0000
Seen: 13 times
Last updated: Jun 18 '23