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

the text method doesn't work in my js file.

// JavaScript Document

  // appending div to the body //


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

     var $image = $("<img>");

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





      //                 AdDiNg OvErLaY aNd iMaGe WiTh TeXt             //




      $image.append($para);

      $overlay.append($image);

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


     // iMaGe CliCk FuNcTiOn ShOwInG oVeRlAy            //


      $(".pic-nav li a").click( function(e) {

           e.preventDefault();

           var loc = $(this).attr("href");

             var captionTex = $(this).attr("alt"); 

            $image.attr("src", loc);

             $para.text(captionTex);


            $overlay.show();

     });

         $image.click( function() {

             $overlay.hide(); 

         });

            ``` js



          this is the whole file.. for the beginning of making a interactive photo album.

3 Answers

David Morisset
David Morisset
13,323 Points

An image can not have children, this is why your paragraph does not appear.

If you want to have an image with a caption, you should use reproduce this kind of markup :

<figure>
    <img src='image.jpg' alt='alt text' />
    <figcaption>Here goes the caption</figcaption>
</figure>

which will translate to :

var $fig = ($("<figure></figure>");
var $image = $("<img>");
var $caption = $("<figcaption></figcaption>");

$fig.append($image);
$fig.append($caption);

Hope this helps

i was trying to select the caption from the alt attr n the html that the a tag with jQuery

i will post the html soon i have to go to work :(