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'>";

10th Jan 2018, 8:18 AM
Ashutosh
Ashutosh - avatar
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!
10th Jan 2018, 8:31 AM
Priyabrata Biswas
Priyabrata Biswas - avatar
+ 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"/>
10th Jan 2018, 8:24 AM
Leon lit
Leon lit - avatar
+ 4
var srcURL = "https://something.com/some.jpg"; var imgbg = '<img class="bgimg" src="' + srcURL + ">"; document.body = imgbg;
10th Jan 2018, 8:45 AM
CalviŐČ
CalviŐČ - avatar