0

I have a JSON element that contains a URL:

"http://media.xyz.com/Data/stockPhotos/17962.jpg"

But when I try to use it in my javascript:

imgRow += "<td><img src=" + item.style.stockPhotoUrl + "/></td>";

The result is:

<td>
    <img src="http://media.xyz.com/Data/stockPhotos/17962.jpg/"/>
</td>

I'm not sure why this is adding a trailing slash.

How can I get rid or this slash?

3
  • 1
    "/></td>" I think you might need a space here " /></td>"? Commented May 15, 2015 at 16:11
  • @thefourtheye - dang! that was it... So simple. Thank you. If you put it as an answer I'll mark it as the answer. Commented May 15, 2015 at 16:13
  • as a side note, you do not need the closing / on the image tag. (see void elements.) Commented May 15, 2015 at 16:15

1 Answer 1

4

HTML tag attributes are supposed to be wrapped in double quotes.

The behavior you are getting is probably a product of not wrapping the src attribute. Try modifying your javascript to this:

imgRow += "<td><img src=\"" + item.style.stockPhotoUrl + "\"/></td>";

or, using single quotes for string representation:

imgRow += '<td><img src="' + item.style.stockPhotoUrl + '"/></td>';
Sign up to request clarification or add additional context in comments.

3 Comments

can also use single quotes ...and if value contains no space quotes aren't essential
Beat me to it. OP, this is what you should be doing. The image tag has quotation marks surrounding the url which you do not include in your code. @charlietfl they may not be essential, but down the road, that url changes and includes a space, and the image won't be seen again
Obviously browsers are forgiving in what they'll allow - we can file this under "good practices". Does anyone happen to use single-quotes as their standard?

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.