So I have this code and would like to know how I could go ahead to put my javascript var into this string. I cannot seem to make a working code for myself.
For the image
You cannot use "
& '
this together unless you escape it with a \
.
var text = '<img src="' + picture.value + '">'
OR
var text = "<img src=\"" + "Hi" + "\">"
Assuming your picture.value
is not empty.
var text = '<img src=' + picture.value + '>';
Example: https://jsfiddle.net/no293etL/
Try using ES6
new feature called Template literals (using quote
). It is more cleaner and simpler.
var picture = {'value':'test-src-location'};
var text = `<img src="${ picture.value }">`;
console.log(text);