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!

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

Why does the element we are appending to appear as a string, but the passed variable containing the new element doesn't?

In this code challenge for Adding New Elements to the Dom, we are asked to create a new element, store the element in a variable and append it to another element in the DOM. When we pass the variable it does not have quotes around it, but the target element we are appending to does. Why is this?

1 Answer

Steven Parker
Steven Parker
224,836 Points

It would help if you showed the actual code, but it sounds like you are describing how you put quotes around strings that are passed as arguments to the jQuery ($) function. jQuery accepts strings of HTML code to create new elements, or strings of CSS=style selectors to identify existing ones. Either will return a jQuery object that can be stored in a variable.

These variables, and JavaScript variables in general, are never enclosed in quotes. That would make them string literals that only represent their own names instead of variables that represent the values assigned to them.