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

why using .text( text ) automatically nest itself into var $caption = $(ā€œ<p></p>ā€);<p></p>

Hi,

I have problem understanding why simply using .text( text ) method can automatically nest the text into the disembodied element?

var $caption = $("<p></p>");

$("#imageGallery a").click(function(event) {
  event.preventDefault();
  var imageLocation = $(this).attr("href");
   //Update overlay with the image linked in the link
  $image.attr("src", imageLocation);

   //Show the overlay.
  $overlay.show();

  //Get child's alt attribute and set caption
  var captionText = $(this).children("img").attr("alt");
  $caption.text(captionText);

});

1 Answer

Hi Howie,

Simply put... "The text() method sets or returns the text content of the selected elements"

http://www.w3schools.com/jquery/html_text.asp