Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript jQuery Basics Understanding jQuery Events and DOM Traversal Adding New Elements to the DOM

Kjetil-Lennart A. Lorentzen
Kjetil-Lennart A. Lorentzen
13,390 Points

Why let $newStudent = $('<li>Sam Smith</li>');

Why use let $newStudent = $('<li>Sam Smith</li>'); Won't this work? let $newStudent = "'<li>Sam Smith</li>'";

I'm trying to understand, but i can't find back to a video where she explained this. Have i skipped something?

3 Answers

Cory Harkins
Cory Harkins
16,486 Points

You aren't wrong, you can do either one, however, using jQuery's syntax you can access jQuery DOM methods that you wouldn't be able to use if you assign $newStudent = "<li>Sam Smith</li>"; as that is a string literal in vanilla javascript.

The benefit to using the jQuery way, is the ability to use DOM Traversing methods such as: .append(), .prepend(), etc...

Antti Lylander
Antti Lylander
9,673 Points

$('<li>Sam Smith</li>') creates an element

"'<li>Sam Smith</li>'" on the other hand is just a string.

Kjetil-Lennart A. Lorentzen
Kjetil-Lennart A. Lorentzen
13,390 Points

Oh i get it now, the jquery way does the "CreateElement()" thing in the script? Ok, i just didn't catch that in the videos, Thanks guys!