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 Working with jQuery Collections Looping through a jQuery collection

tal Shnitzer
tal Shnitzer
Courses Plus Student 5,242 Points

syntax of 'element' argument on each() method in jQuery

in the following code: $('a').each(function(index,element){ console.log(index,$(element).attr('href')); });

why passing element argument to console.log() as "$(element)". and not simply "element". what are the $() stands for?

1 Answer

The each applies your function to each element as a key (stored in index) and its value (stored in element). In order to get the actual element from the DOM you need to convert the element value to the actual element, this is what $() does. It finds the actual element in the DOM referred to by the value in element and returns it so that you can examine its href attribute.