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 trialManish Bhattarai
3,897 Pointsattr
what is the reason behind changing src value to href value?plz explain it
1 Answer
Andrew Shook
31,709 PointsIf you look at the html for the project you will see that the images in the gallery are wrapper in an anchor tag like so:
<a href="mysite.com/images/some-image.png">
<img src="mysite.com/images/some-image.png">
</a>
Andrew then binds a click event to the anchor tag and prevents the browser from navigating to the image:
$("#imageGallery a").click( function(event){
event.preventDefault();
Since the anchor tag is the element being clicked, Andrew Chalkley, needed a way to load the image into the overlay using information from the anchor tag. If you notice, the href value of the anchor and the image src are the same. So he took the value of the href and used it to display the image in the overlay.
That is not the only way he could have done it. Since each anchor has an image inside of it he could have:
//this variable stores the src attribute of the anchors child image
var imageLocation = $(">img", this).attr("src");
$image.attr("src", imageLocation);
or he could have simply copied all the html inside the anchor tag:
// .html() copies all the html inside the matched element. In this case the anchor tag
// that was clicked.
var $image = $(this).html();
The point is, Andrew needed a to access the src of the image in order to display it in the overlay. He chose to use to href of the anchor element (which is the same as its image's src) to get that value.