0

In my script, text(message.image) returns a dynamic URL path of an image. What would be the correct statement in a javascript?

This fails:

 $('<div/>').'<img src=\"'.text(message.image).'\">'.appendTo($('#msgDiv'));
4
  • 2
    Do you mean javascript? Commented Aug 20, 2014 at 10:57
  • Yes... javascript @Pphoenix Commented Aug 20, 2014 at 11:02
  • Are you getting any error? Commented Aug 20, 2014 at 11:02
  • For concatenation in Javascript, + is to be used. Commented Aug 20, 2014 at 11:10

4 Answers 4

2

The real answer is:

$('<div><img src="'+message.image+'"/></div>').appendTo($('#msgDiv'));

You have a couple syntactic errata in your code snippet:

  • You can't access a property with a string like you do.
  • Concatenation of strings is not with a dot but with a plus.
  • You are trying to execute text() on a string, not on the div.
Sign up to request clarification or add additional context in comments.

Comments

2

I think you're missing (),+,append method and escaping " incorrectly, try with this:

$('<div/>').append('<img src="' + text(message.image) + '"/>').appendTo($('#msgDiv'));

Hope this helps,

Comments

0

I think you have a problem because you're using (double)quote badly

You escape the double quote but you're using simplequote. Try that :

$('<div/>').'<img src=\''.text(message.image).'\'>'.appendTo($('#msgDiv'));

And are you sure concerning the syntax $('').'SOME HTML CODE' ?

Comments

0

You are missing ( and ) there.

Also, you dont need to escape double quotes since you are using single quotes outside.

Try this:

$('<div/>').('<img src="'+text(message.image)+'">').appendTo($('#msgDiv'));

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.