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 4

Rossella Rosin
Rossella Rosin
15,734 Points

why use text() and not append() for the caption too?

Is there any particular reason, apart from teaching us a different method, why we used

$caption.text(captionTxt);

instead of

$caption.append(captionTxt);

which seems to work fine as well, at least on chrome. After all, a text node is always a node.

1 Answer

Steven Parker
Steven Parker
231,269 Points

It might appear to work the same, but only because the element had no text to begin with.

  • .text() completely resets the element text. If there was any there before, it is replaced with the new.
  • .append() adds an additional text node to the element. Any text already there will still be seen with the new.

Try using append and see what happens when you use the overlay a second time (or third, etc).