0

I am trying to figure out how to convert a user entered string which contains an image url, to display as an image by adding the img attr.

Here is what i have been playing with:

$(".message_content").each(function() {
    var text = $(this).text();
    text = text.replace("http://www.example.org/myimage.jpg", "<img src="http://www.example.org/myimage.jpg"> ");
    $(this).text(text);
});

Although this doesnt work, anybody tried anything similar?

1
  • 1
    What is the string entered by the user? Commented Aug 15, 2014 at 15:00

1 Answer 1

3

Escape the ".

text = text.replace("http://www.example.org/myimage.jpg",
  "<img src=\"http://www.example.org/myimage.jpg\"> ");

More general:

var userinput = "http://www.example.org/myimage.jpg";
text = text.replace(userinput, "<img src=\"" + userinput + "\">");
Sign up to request clarification or add additional context in comments.

1 Comment

Yeah or use single quotes in your JS: text = text.replace('http://www.example.org/myimage.jpg', '<img src="http://www.example.org/myimage.jpg">');

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.