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
Jeff Heaton
6,800 PointsWhy can't I write captionText variable as var imageCaption = $(this img).attr('alt');?
We use the jQuery selector earlier like this, but don't use 'this'. Is 'this' not valid for jQuery because it's not a CSS element?
2 Answers
Dave McFarland
Treehouse TeacherHi Jeff Heaton
You'll often use $(this) inside of an event callback, or inside of a .each() callback. I'm not sure exactly what video or step you're looking at but let's assume that you've selected a series of div tags that contain images in them. Each of those divs has a class named imageContainer. You could then select those divs like this:
$('.imageContainer');
Then, to act on those divs you could use jQuery's .each() method like this:
$('.imageContainer').each( function () {
});
This function runs once for each div -- inside the function you add the programming you want. If you would like to get the image inside the div and then get that image's alt value you'd do this:
$('.imageContainer').each( function () {
var imageCaption = $(this).find('img').attr('alt');
// add the rest of your programming here
});
Hope that helps.
Jeff Heaton
6,800 PointsThanks for the speedy reply! Sorry for not referencing a video or timestamp.
Dave McFarland
Treehouse TeacherNo problem Jeff. Hope that helps.