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 3

Dayne Wright
Dayne Wright
11,235 Points

How does it know to put the <img> inside the <div>?

var $overlay = $('<div id="overlay"></div>');
var $image = $("<img>");

$overlay.append($image);

//Add overlay
$("body").append($overlay);

In the example above, how does jQuery know to put the img inside of the div?

2 Answers

Michael Hulet
Michael Hulet
47,911 Points

When you're declaring your $overlay & $image variables, don't think of them as simple strings. Think of them as actual HTML elements that you haven't yet added to your page's DOM. The append function of jQuery puts one element inside another, after everything that's already in the element you're inserting something into. In this case, you have a new div, which you're calling the append function on, and you're telling that div to add an img to the very end of everything that's already in it