0
How to use a javaScript variable into another?
// I want to use srcURL's value as a src to img tag. How to do it? var srcURL = "https://something.com/some.jpg"; var imgbg = "<img class='bgimg' src='srcURL'>";
3 Answers
0
var srcURL = "https://something.com/some.jpg";
var img = document.createElement("img");
var body = document.getElementsByTagName("body")[0];
//assuming you didn't already created <img> tag
img.setAttribute("src", srcURL);
body.appendChild(img);
/*assuming that's where you want to put it!
Or you could've created another <div> tag in your element for putting your image*/
That's how you do it by manipulating DOM!
Hope I was able to clarify your issue!
+ 4
try this in your js
srcValue="https://upload.wikimedia.org/wikipedia/commons/thumb/0/03/Vulpes_vulpes_laying_in_snow.jpg/280px-Vulpes_vulpes_laying_in_snow.jpg"
window.onload=function() {
document.getElementById("test").src=srcValue;
}
in HTML just put
<img id="test"/>
+ 4
var srcURL = "https://something.com/some.jpg";
var imgbg = '<img class="bgimg" src="' + srcURL + ">";
document.body = imgbg;