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 trialbill curry
Front End Web Development Techdegree Student 7,841 PointsI don't understand the use of (this) in this project. Any help?
I remember using "this" in earlier projects and it seemed to refer directly back to a preceding element. Not sure how it's working here in these two instances.
Any clarification would be most appreciated.
//1. Capture the click event on a link to an img
$("#imageGallery a").click(function(event) {
event.preventDefault();
var imageLocation = $(this).attr("href");
// update overlay w/the img linked in the link
$image.attr("src", imageLocation);
// get child's attribute and set caption
var captionText = $(this).children("img").attr("alt");
$caption.text(captionText);
1 Answer
Steven Parker
229,786 PointsThe use of "this" depends on the context. In an event handler, it refers to the element that dispatched the event. In this case it would be the link ("a") element that was clicked.
You might enjoy the short workshop: Understanding "this" in JavaScript.
bill curry
Front End Web Development Techdegree Student 7,841 Pointsbill curry
Front End Web Development Techdegree Student 7,841 PointsThanks. Will check it out.