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

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,500 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,686 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!