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 Spoiler Revealer Perform: Part 2

Sven Huff
PLUS
Sven Huff
Courses Plus Student 4,355 Points

Cant's understand function syntax jquery…

Hi there, I don't understand the brackets in the "show spoiler" button:

$("button").click(function(){ $(".spoiler span").show(); });

I don't get, why I have to end the functions rule inside the "{}" brackets with this one ")".

Thank's for your help in advance.

Best,

Sven

3 Answers

Jonathan Grieve
MOD
Jonathan Grieve
Treehouse Moderator 91,252 Points

Hi Sven

$("button").click(function(){ $(".spoiler span").show(); });

If you look at the function keyword, it's an event handler that's bounded itself to a button element, so it's going to perform something on that element.

If you look at how the keyword is structured (function() there's a second opening bracket there that needs to be closed at the end of the function block.

That's why it's closed like this });

Good luck!

Sven Huff
PLUS
Sven Huff
Courses Plus Student 4,355 Points

Hi Jonathan,

many thanks for your answer. I just typed, what I thought is more logical – but than it becomes clear. Just for clarification: "The key function keyword is an event handler, that's bounded itself to a button element" – that is, why the function looks, how it looks. Otherwise, it would be kind of that:

… .click(function) { $(".spoiler span").show(); }

Best,

Sven

Ryan Field
Ryan Field
Courses Plus Student 21,242 Points

Kind of. This is the breakdown of the jQuery:

$("button").click(  //<-- We're saying we want something to happen when the button is clicked. 'Click' parentheses open here.

// Everything that happens here is what we want to happen when the button is clicked. We use an anonymous function to do this.

function(){  //<-- Anonymous function curly braces open here.

$(".spoiler span").show();

} //<-- Anonymous function curly braces close here.


); //<-- 'Click' parentheses close here.

Condensed, it looks like this:

$("button").click(function(){
  $(".spoiler span").show();
});