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
Alexey Tseitlin
5,866 PointsjQuery basics
Whats the benefits of doing this:
var overlay = $("<div>Hello</div>");
instead of this:
var overlay = "<div>Hello</div>";
1 Answer
zazza
21,750 PointsThe first creates a jQuery object and you can use all (most) of the jQuery methods, extensions, etc. on that object. The second simply creates a plain JavaScript string (it's only a coincidence that they contain the same "string value") with only the methods/properties provided by JavaScript (e.g., length property, or toLowerCase() method). Simply put, the second one has nothing to do with jQuery, it's just a plain old string.
Sally Gradle
24,694 PointsSally Gradle
24,694 PointsI agree with George. If all you want is a string, then the second option will work. However, if you want to use the object, then it has to be a jQuery object. It's all in what you are trying to accomplish. I find it a little easier to find my way (write the program) if I know where I want to go (what result I want). Keep that in mind, and you will know which to use.