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 (2014) Creating a Simple Lightbox Perform: Part 1

Michele Kempinsky
Michele Kempinsky
12,201 Points

Need 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
Steven Parker
229,644 Points

You'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")   :point_left: select all the links (a) that are inside the imageGallery
.click(   :point_left: make something happen when they are clicked on
function(){   :point_left: run this function when one is clicked
var href=   :point_left: store something in the variable href
$(this)   :point_left: look at the specific link that was clicked
.attr("href");   :point_left: get the value of the href attribute it has (and store that)
console.log(href);   :point_left: now write that out to the console
});   :point_left: that's the end of the function (and the click handler)

Is it clear now?

William Bok
William Bok
6,842 Points

That was a great breakdown thanks :)