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 3

Yijiao Wang
Yijiao Wang
1,793 Points

Why are we using href instead of src to include the image?

var imageLocation=$(this).attr('href'); $image.attr('src',imageLocation); $overlay.show();

For this part of the code, I don't understand why we update the image src with href values? What's the difference between using src and href in this case? Why can't we either use image.attr('href') or image.attr('src') or image(imageLocation)?

1 Answer

Arturo Alviar
Arturo Alviar
15,739 Points

If you look at the html you can see that the <a> tags have an href value that refers to an image. Images do not have href attributes. As you know, they only have src values which define the location for an image. What the code does is, it gets the href value of the clicked <a> tag and makes that href value the src of the image. Simply put, the a tag's href value just points to where a image is so when a user clicks on the link, the image that we build using jQuery has a src value. Hope this helps!

Yijiao Wang
Yijiao Wang
1,793 Points

Thank you! your reply is very helpful

I couldnt understand((