2

When I do this:

$('.myDiv').html("<strong>Hello World</strong>");

The output inside myDiv is literally:

<strong>Hello World</strong>

instead of Hello World simply being bolded. Is there any way to use html inside the html() function or a similar function that actually translates html instead of showing it literally?

EDIT

oops I just realized the above code actually does work. My problem was I am trying to replace the contents of a textarea with html inside and that echoed out the html because that's what happens when you put html inside a textarea anyway. So nothing to do with jQuery.

2 Answers 2

4

Are you sure you're not using .text() ?

$('.myDiv').html("<strong>Hello World</strong>"); works for me.

See this jsFiddle.

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

Comments

0

It will display Hello World in bold when the browser actually renders the HTML. You must be using Firebug or a similar tool to inspect the contents of the div you modified. Tools like that don't render the content you are trying to inspect the way the browser would; that's the whole reason you are inspecting the markup. If they did render what you were trying to examine, well, that would just be cruel.

4 Comments

He said the output is <strong>Hello World</strong>
I don't think that's what he is saying. I think he is trying to inspect the result using a debugging tool and is surprised that the tool is not rendering the content. html() doesn't look much like text() so I don't think he is mixing the two up.
So what you're saying is that he's expecting the text to be bold inside the Firebug source?
Yes that's what I was expecting. However, the asker has added a clarification: My problem was I am trying to replace the contents of a textarea with html inside and that echoed out the html because ... Ok, so it's not a debugging tool he was using, but he was still using html(). Similar mistake though.

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.