0
<label id="lbl"></label>

I'm trying to get this image to display based on the variable "a" containing the String of the URL. Can someone help me with the missing steps for this?

var a = "https://encrypted.google.com/images/logos/ssl_logo.png";

document.getElementById("lbl").innerHTML =  '<img src=a >';
Jacob
  • 77,566
  • 24
  • 149
  • 228
  • Does this answer your question? [How can I do string interpolation in JavaScript?](https://stackoverflow.com/questions/1408289/how-can-i-do-string-interpolation-in-javascript) – John Montgomery Jun 03 '20 at 22:04

4 Answers4

1

You have .innerHTML = '<img src=a >';, which is going to have the src attribute set to the literal value a. You instead want to add the value of the a variable to your HTML. Luckily, nowadays string templates make that easy:

document.getElementById("lbl").innerHTML = `<img src="${a}" />`;

Inside of "backtick" characters, anything appearing in a ${...} expression gets evaluated and injected into the string. Notice I also surrounded your src attribute value in quotes; although HTML parsers are forgiving, omitting that could cause problems.

Jacob
  • 77,566
  • 24
  • 149
  • 228
0

You simply concat the strings.

document.getElementById("lbl").innerHTML = '<img src="' + a +'" \>';

Subir Kumar Sao
  • 8,171
  • 3
  • 26
  • 47
0

You need to create an img element with the src of the value of a and append it to the label:

var a = "https://encrypted.google.com/images/logos/ssl_logo.png";
let img = document.createElement("img");
img.src = a;
document.getElementById("lbl").appendChild(img);
<label id="lbl"></label>

However, you can fix your code by using template literals:

var a = "https://encrypted.google.com/images/logos/ssl_logo.png";
document.getElementById("lbl").innerHTML =  `<img src=${a}>`;
<label id="lbl"></label>
Majed Badawi
  • 27,616
  • 4
  • 25
  • 48
-1

You should interpolation strings. <img src=${a} > instead of '';

Antonio Gamiz Delgado
  • 1,871
  • 1
  • 12
  • 33