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 (2014) Creating a Simple Lightbox Perform: Part 4

Arikaturika Tumojenko
Arikaturika Tumojenko
8,897 Points

How do we select elements (variables in particular) with Jquery?

I see that elements are selected using two different methods. Like this

$("body").

and like this

$overlay.

I tried changing $overlay to $($overlay) and it seems to be working the exact same way. So what's the difference?

2 Answers

You are correct in that elements are selected like this:

$('body')

If you are going to use this selection multiple times throughout your code, for the sanity of the developer, you create a variable so you don't have to type $('body') every time the body selection is needed.

You can create a variable out of this like this:

var body = $('body');

You can also create the variable that is prepended the $:

var $body = $('body');

Both are the right way to do it. The second construct is a way for the developer to easily look at the code and know it is a jquery object instead of a variable with a different data type. You can certainly create the variable without the $ like var body. That does not stop you from using it as a jquery object like body.append();. The $ at the beginning of the variable is for code readability. It's more of a personal preference issue.

$overlay equals $($overlay), is a redundant statement. $overlay is a variable containing an element which in this case would be like var $overlay = $('#overlay');

You could just select it like this $('#overlay') but assigning it to a variable helps with readability.

Aldo don't get confused with the jQuery dollar sign and the variable dollar sign which is just a common practice of some developers, you could just assign a variable name without the dollar sigh like this var overlay = $('#overlay');

Check out this question for more info:

http://stackoverflow.com/questions/205853/why-would-a-javascript-variable-start-with-a-dollar-sign

Arikaturika Tumojenko
Arikaturika Tumojenko
8,897 Points

Ok, maybe it wasn't the best example. If we have this code

var houses

we would select it like this (if it's to judge based on this video)

houses.

My questions is why? Why don't we select it like this

$(houses).

which is basically the same thing? It seems to me like the dollar sign is randomly used when selecting with JQuery. Or maybe I am wrong, so that's why I am asking.

P.S: I didn't get confused with the $overlay variable, I know why we used the dollar sign in the beginning.