Snowlav
Snowlav

Reputation: 465

"posting" an image in jquery

I currently use this little line of code to "post" messages.

setTimeout(function(){ 
    o.html(o.html() + "Username:<br>" + msg[r] + "<br><hr>") 
}, 7000);

It works great, it posts messages from an array, ads some styling.

But right now when it posts a message it looks like:

Username:
messagehere

<hr>

What I want is the code to post an image as well, before the message and username. Like as you would see on facebook when chatting with someone, commenting something etc.

I tried just putting a +<img src="" class=""/> in front of it but that did nothing. The whole code suddenly stopped working.

So right now I am looking for the correct way of doing this, id like to add an class to it, so I can style the whole thing correctly with css (having the avatar in front of the message and username, like explained earlier)

If someone could help me find the correct way, that'd be much appreciated.

Upvotes: 0

Views: 45

Answers (1)

Chad
Chad

Reputation: 1549

You just need to wrap the img tag in quotes. It's erroring because without the quotes, your browser thinks it's a variable or function instead of a string literal.

+ '<img src="" class=""/>'

Also, you should use the .append() function instead of doing .html(.html() + ...)

o.append("Username:<br>" + msg[r] + "<br><hr>" + "<img src='' class='' />"); 

Upvotes: 2

Related Questions