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 trialMichele Kempinsky
12,201 PointsNeed help grasping this concept.
$("imgageGallery a").click(function()){ var href= $(this).attr("href"); console.log();
I'm having a little trouble breaking down and understanding this piece of code. I understand that $("imgageGallery a").click(); is an event method. But, can someone break down the rest a bit more? Why are we using a function here? What is the $(this) really doing?
Please help slow it down a little for me :) thanks!
2 Answers
Steven Parker
231,269 PointsYou've got an extra parenthesis in there, and you misspelled "imgage", but I know what you mean. Whenever you set up an event method, you pass it a function to perform any time that event happens.
I'll try to break it down:
$("#imageGallery a")
select all the links (a) that are inside the imageGallery
.click(
make something happen when they are clicked on
function(){
run this function when one is clicked
var href=
store something in the variable href
$(this)
look at the specific link that was clicked
.attr("href");
get the value of the href attribute it has (and store that)
console.log(href);
now write that out to the console
});
that's the end of the function (and the click handler)
Is it clear now?
William Bok
6,842 PointsThat was a great breakdown thanks :)