JavaScript jQuery Basics Introducing jQuery Blog Previewer: Getting Values from Form Fields

Mahfuzur Rahman
Mahfuzur Rahman
3,162 Points

.text(title) but .html(content)

why are we using $('#blogContentPreview').html(content) instead of $('#blogContentPreview').text(content)

1 Answer

Steven Parker
Steven Parker
97,859 Points

The "html" method allows the content to contain markup tags (such as the "<strong>" shown in the example), but the "text" method does not. If you were loading the content with "text", then the "<strong>" tag would show as-is instead of causing the area it encloses to be emphasized.

If you wanted to allow markup to be used in the title, you could substitute the "html" method there also.

A little experimentation might be worth more than any explanation!

Mahfuzur Rahman
Mahfuzur Rahman
3,162 Points

Thanks a lot! Actually I tried both, but without the <strong> and got the same output and hence I asked for help :)