You need to pass the function reference
as a click handler, but what you are doing here
$("#Button1").bind("click", { key1: "value1", key2: "value2" }, buttonClick(event));
calls buttonClick(event)
immediately and which return undefined
and sets it as a click handler
. You must pass a link to a function, for example buttonClick
, and you will automatically receive an event parameter (jQuery will send this when the handler is called).
Full code:
$(function(){ $("#Button1").bind("click", { key1: "value1", key2: "value2" }, buttonClick); function buttonClick(event) { $("#displayArea").text(event.data.key1); } โ});
Demo: http://jsfiddle.net/joycse06/cjc9r/
Update (based on @Tadeck comment):
Your code will work fine if you use function expression
like this
var buttonClick = function(event){ $("#displayArea").text(event.data.key1); };
And you should place this over its first use
. In this case, before
$("#Button1").bind("click", ...
Because function expressions
not hoisted
at the top of the current scope, e.g. function declaration
. Therefore, you can use them only after
expression
was interpreted
by the JS interpreter
.
source share