2

I'm using the jQuery hotkeys plugin, written by John Resig (Mr jQuery). It uses bind, however, and bind is now replaced by on as noted in the official jQuery API.

The standard code is:

$(document).bind('keyup', 'alt+a', function(evt){...});

I tried changing bind to on, but it didn't work. Is there a workaround, or does it not matter?

I'm also wondering whether attaching the handler to the document is always going to be the best way.

ahsteele
  • 26,243
  • 28
  • 134
  • 248
Nick
  • 5,995
  • 12
  • 54
  • 78

2 Answers2

4

on take an extra DOM arg, http://api.jquery.com/on/, so write null.

try

$(document).on('keyup', null, 'alt+a', function(){}
Jonathan de M.
  • 9,721
  • 8
  • 47
  • 72
0

The plugin redeclares the use of the .bind and .unbind methods of jQuery as seen in these lines

jQuery.fn.bind = function(){
jQuery.fn.unbind = function(){

We can simply do

jQuery.fn.on = function(){
jQuery.fn.off = function(){

This also needs to be changed -

jQuery.fn.__bind__ = jQuery.fn.bind;
jQuery.fn.__unbind__ = jQuery.fn.unbind;

To

jQuery.fn.__bind__ = jQuery.fn.on;
jQuery.fn.__unbind__ = jQuery.fn.off;

It's reused a couple times, so replace any occurence of bind note -- Do not replace __bind, just bind occurences with on, and unbind occurences with off.

Or just use the code in my jsFiddle

Ohgodwhy
  • 49,779
  • 11
  • 80
  • 110
  • Thanks for the answer, although my copy of the plugin didn't make any reference to either `bind` or `on` except in the implementation lines. I've gone with Jonathan's answer. – Nick Dec 26 '12 at 09:42