Vlad Vlad - 3 months ago 23
Javascript Question

How can I send custom data to JQuery click event?

I have this JQuery click event:

jq("#generateButton").click({key: "hello world"}, function () {
console.dir(this);// this is the "generateButton" html element.
// jq(this) would be the jquery object of the generate button
frnConstr.setDataObject(this);
frnConstr.gameObject.state.start(frnConstr.EnteriorScreen.KEY);
});


How could I possibly send custom data to this event, for example print {key: "hello world"}?
Is it at all possible?

Note: In my custom made working envirionment jq() is $() or jquery().

Note: JQuery docs say that eventData can be anything: https://api.jquery.com/click/ :)

Answer

First, make sure you are reference jquery version 1.4.3 or higher.

jq("#generateButton").click({key: "hello world"}, function (event) {
    var key=event.data.key;
    //do something with the key value
});

All data passed in this eventData signature are accessible in the event.data object.