Skip to content
Advertisement

Displaying an image dynamically in a html table in JavaScript

I am trying to display an image in a table. This is what I have tried:

imgv = "/img/koala.jpg";
dcontent = "<table style='background-color:purple;'><tr><td style='width: 100px; color: red;'> Date </td>";
                    dcontent += "<tr><td style='width: 100px;'><img src="' + imgv + '"/></td>";

I am getting a compile time error in the second line:

“; expected”.

I tried many syntax like:

<img src=" + imgv + "/></td>

but nothing is getting compiled.

Advertisement

Answer

Correct answer is replacing "' + imgv + '" with '" + imgv + "'

imgv = "/img/koala.jpg";
dcontent = '<table style="background-color:purple;"><tr><td style="width:     100px; color: red;"> Date </td>';
dcontent += "<tr><td style='width: 100px;'><img src='" + imgv + "'/></td>";
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement