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

Aaron Selonke
Aaron Selonke
10,323 Points

calling the show() method directly on the variable

In the video, Andrew calls the show method directly on the jquery formated string variable like this

$overlay.show();

That kind of threw me offgaurd; I would natualry write it like this

$(#overlay).show();

I'm curious, if we can call this method directly on the variable like that, why can't we call show() directly on the string or the JQ formated string?

both of these do not compile in the browser:

'<div id="overlay"></div>'.show();
$('<div id="overlay"></div>').show();

Why is that? Isn't this the same value as the variable?

3 Answers

Benjamin Barslev Nielsen
Benjamin Barslev Nielsen
18,958 Points
'<div id="overlay"></div>'.show();

We cannot call show directly on the string, since show is a method defined on jQuery's wrapped set, i.e., show is not defined on strings.

$('<div id="overlay"></div>').show();

This is valid jQuery, but you don't see any changes in the browser, since the newly created div hasn't been added to the DOM yet, and the browser only displays what is in the DOM. Therefore you need to use the appendTo method to see the result in the browser:

$('<div id="overlay"></div>').appendTo('body').show();
Aaron Selonke
Aaron Selonke
10,323 Points

Is there anything wrong with calling it on the ID

$(#overlay).show();
Benjamin Barslev Nielsen
Benjamin Barslev Nielsen
18,958 Points

There is nothing wrong with calling it on the ID, but remember that $(selector) takes the selector as a string, so the statement should be:

$('#overlay').show();
Justin Sze Wei Teo
Justin Sze Wei Teo
9,418 Points

Hi Benjamin (or whoever else can answer this question),

Since

$('<div id="overlay"></div>').show();

does not work because you mentioned "the newly created div hasn't been added to the DOM yet".

Are you implying that by first declaring the variable, i.e., (1) var $overlay = $('<div id="overlay"></div>');

and then using the declared variable to immediately call show i.e., (2) $overlay.show();

Doing this adds the newly created div to the DOM? If so, which step in the above was the newly created div added to the dom? I'm assuming it was in (1) above?

Kindly advise, thanks!

Benjamin Barslev Nielsen
Benjamin Barslev Nielsen
18,958 Points
var $overlay = $('<div id="overlay"></div>');
$overlay.show();

is exactly the same as:

$('<div id="overlay"></div>').show();

so the div is not displayed in the browser in either of these cases. To display it we need to add it to the DOM with appendTo:

var $overlay = $('<div id="overlay"></div>');
$overlay.appendTo('body');
$overlay.show();

Hope this helps

Aaron Selonke
Aaron Selonke
10,323 Points

Thank you I'm looking into the 'jQuery wrapped set' to see what that's all about.

Justin Sze Wei Teo
Justin Sze Wei Teo
9,418 Points

Yes! That helps, i didnt notice the .append to body. Makes sense now thanns