Vlad Vlad - 1 year ago 242
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

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 Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download