tags:

views:

41

answers:

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.

A: 

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.

SimpleCoder
He said the **output** is `<strong>Hello World</strong>`
Marko
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.
SimpleCoder
@SimpleCoder - So what you're saying is that he's expecting the text to be bold inside the Firebug source?
Marko
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.
SimpleCoder
+4  A: 

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

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

See this jsFiddle.

Marko
Oh my, jsFiddle rocks.
Martinho Fernandes
See my edit, I see html() function does translate html.
Sammy