Hasen
Hasen

Reputation: 12324

Jquery hotkeys can't send value with function

I'm wondering why this works with jquery hotkeys:

$(document).bind('keydown', 'm', dothis);

but this doesn't:

$(document).bind('keydown', 'm', dothis(6));

Even like this doesn't work:

$(document).bind('keydown', 'm', dothis());

Upvotes: 0

Views: 52

Answers (2)

user6748331
user6748331

Reputation:

You can also use curried function. Its function which returns function.

function doThis (num) {
  return function () {
    // Do something with num
  }
}

$(document).bind('keydown', 'm', dothis(6))

Upvotes: 0

jlaitio
jlaitio

Reputation: 1948

You must give the binding a function as a parameter.

In the second and third cases you are giving a function call which gets evaluated, and the returned value is given to the keybind function - presumably you do not return a function and that does nothing.

If you want to construct a new function with set parameters from a function you already have, use bind:

$(document).bind('keydown', 'm', dothis.bind(null, 6));

Upvotes: 2

Related Questions