Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
James March
7,921 Points.html() rather than .text()
In what situation would I use the .text() method in jQuery over .html()?
Seems to me that .html() is more versatile and can do more?
3 Answers
Steven Parker
243,318 PointsOne good reason to use "text" is when you are displaying something that was entered by the user, and you do not want to allow them to create HTML elements. This is done right here on the forum. At one time, it was possible to include HTML tags in forum postings but that doesn't work any more now that the input is handled as text instead.
Amber Fuller
4,598 Points.html will treat a string as HTML, where .text will treat the string as text. Here's a discussion on stackoverflow about it https://stackoverflow.com/questions/1910794/what-is-the-difference-between-jquery-text-and-html
James March
7,921 PointsThanks for these, appreciated