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 Creating and Appending Detached DOM elements

Farid Wilhelm Zimmermann
Farid Wilhelm Zimmermann
16,753 Points

Why cant I use $('#modal').append?

In this code challenge, we were supposed to append a dismiss button unto a <div> with the id="modal". This div gets saved as a jQuery Object into a variable called $modal.

obviously, ----- $modal.append($button); ----- works to append the button object unto the modal,

but when I tried selecting the <div> using its #id name like $('#modal').append($button);

So do I have to predefine jQuery objects as variables to use further methods on them?

1 Answer

Steven Parker
Steven Parker
229,744 Points

The reason "$('#modal').append($dismissButton);" would not work is that you are attempting to do it before the modal div has been placed on the document.

:point_right: If you put it after line 16 (when $modal is appended to the document body), it does work.

Farid Wilhelm Zimmermann
Farid Wilhelm Zimmermann
16,753 Points

thanks a lot mate, that cleared up the confusion!