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 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,842 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