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 trialKevin Narain
11,379 PointsI don't understand what $(this) is doing in this context.
To what is $(this) reffering to?
// For each <a> element you should...
$('a').each(function() {
const url = $(this).attr('href');
$(this).parent().append(`(${url})`);
});
1 Answer
Jennifer Nordell
Treehouse TeacherHi there, Kevin Narain ! The $(this)
is referring to the current <a>
element the loop is looking at. Assuming there are 5 <a>
elements on the page, that each()
is going through each one one at a time. That means that in each iteration, the $(this)
will refer to that anchor element.
The first iteration $(this)
will be the first anchor element, and the next iteration the second anchor element and so forth.
Hope this helps!
Kevin Narain
11,379 PointsKevin Narain
11,379 PointsHi Jennifer,
Thanks for your explanation I finally got it down 💡