1

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.

2 Answers 2

3

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>";
Sign up to request clarification or add additional context in comments.

1 Comment

You still need to surround the src with double quotes. The style attributes as well. I suggest flipflopping the single and double quotes.
2

You need to replace "' + imgv + '" with '" + imgv + "', because ' is stronger than ". For example, when you create variable named foo:

var foo = "write sentence 'like' that";

in html you will get just "write sentence that".

SOLUTION is to replace them, then

var foo = 'write sentence "like" that'

will output exactly write sentence "like" that - nothing wrong :-)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.