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
gerardo keys
14,292 PointsWhy did you use .text() instead of .html() to set the title and paragraph in the click handler?
When is it better to use .text() vs .html()? It seems like they both have the same effect. Why would you use one over the other? Should you stay consistent on which one you choose?
2 Answers
Steven Parker
243,266 PointsThe choice should be based on the contents. If you are inserting content that contains HTML tags, you would use the "html()" method. Necessary elements are created automatically to implement the HTML.
Oh the other hand, if you are inserting pure text, the "text()" method prevents any of the content from being misinterpreted as markup.
Examples:
divOne.html("<h1>Heading Tags</h1>");
divTwo.text("Heading tags look like this: <h1>Sample</h1>");
And the output would look like:
Heading Tags
Heading tags look like this: <h1>Sample</h1>
gerardo keys
14,292 PointsThank you! This definitely clears it up.