Hi guys,
I’m a newbie to JQuery. I don’t understand the following part of the JQuery syntax in red:
$(‘button’).click(function() {
$(this).fadeOut('slow);
});
Why is it
click(function() {
});
and not just .click({
});
? I don’t understand the “function” text. Thank you.
In javascript you can pass a function as a parameter like this so that the code within is executed later.
That part in red is actually ordinary JavaScript syntax. It’s a function expression. jQuery’s click method takes a callback – that is, a function – to execute when the click event occurs.
If we were to write it out more verbosely…
var buttonClickHandler = function () {
$(this).fadeOut('slow');
};
$('button').click(buttonClickHandler);
jQuery is just a library of pre-written JavaScript functions so you really need to know at least the basics of JavaScript in order to be able to use jQuery.