Reputation: 21842
I am trying to bind a click function to an HTML element. So i tried this :-
$("#buttonId").click(alert("hey")); // does not work.
And then i tried this :-
$("#buttonId").click(function() {
alert("hey");
}); // works perfectly fine.
Since alert is also a function, why i can't directly pass it. Can somebody please put some light on this ?
Click method documentation is available here http://api.jquery.com/click/
Upvotes: 0
Views: 80
Reputation: 4400
The click
method take an argument of handler type while alert("hey");
is a function call that is neither a handler no it returns a handler, that'y this call is invalid .
Upvotes: 1
Reputation: 75317
In the second example you're passing a function to the click()
method but not calling it. When someone clicks on the element, jQuery will then call the function for you.
In the first example however, you're calling "alert("hey")
" (which returns undefined
), and that value is passed to the click
method.
Upvotes: 5