beginner beginner - 1 month ago 4
HTML Question

Why .html() don't output html tags?

If i have a p element

<p> simple text <h3> this is h3 text </h3> </p>


And i do this.

alert($('p').html());


It should give output
simple text <h3> this is h3 text</h3>


But it alerts only
simple text
. I thought Ok may be
.html()
does not output html tags

But when i add any html using jquery in same p element , And then alert in same way it gives me result which is expected(it shows html tags as well) .

This is example

https://jsfiddle.net/04f1drby/

Can someone explain why this different behaviour ? Thanks in advance

Answer

If you go into chrome inspector for the JS fiddle page you linked you'll notice the HTML gets rendered as this:

<p id="firstp">
simple text</p><h3>
this is h3 text
</h3>
<p></p>

A closing paragraph tag is added just before your starting header tag.

Comments