4

I could append a string to my table to show the status as such

$('#test').empty().append("on")

but it would be better if i can display an image instead of a string. i tried this:

$('#test').empty().append(<img src="/static/on.png" height="64px" width="64px">)

but it's not working. How should i do it?

1
  • 2
    The entire <img /> tag needs to be in quotes. $.append('<img src="..." />') Commented May 8, 2013 at 3:05

3 Answers 3

11

Missing quotes:

$('#test').empty().append('<img src="/static/on.png" height="64px" width="64px">');

Since you are emptying and appending item. you could do this instead. .html will replace the whole content of your selector with image item. Later on if you want to append to this div you can do .append(...);

$('#test').html('<img src="/static/on.png" height="64px" width="64px">');

If you are planning to empty and append image then .html() is the best approach for you.

See Ref for .html()

Sign up to request clarification or add additional context in comments.

2 Comments

It would still need quotes. And the $ sign. :)
@PSL could you help me take a look at this question as well? :) stackoverflow.com/questions/16411085/…
8

Try

$('<img />', {
    src: 'test.png',
    width: '200px',
    height: '100px'
}).appendTo($('#empty').empty())

3 Comments

Thanks again Arun! Could you help me take a look at this problem here too? stackoverflow.com/questions/16411085/… :)
with this code, img becomes style="width:200px; height:100px" attribute
I need to add style background-image but it doesnt get inside the style parameter. $('<li />', { background-image: 'url("'+tweet.photoUrl+'")', width: '134px', height: '134px' }).appendTo($('.tweets')); Wont understand background-image
3

You need to surround the HTML to be appended with quotes:

$('#test').empty().append('<img src="/static/on.png" height="64px" width="64px">');

I used single quotes here because you already have double quotes in the HTML, so they would escape the string if you used doubles to surround it.

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.