Nick
Nick

Reputation: 6025

jQuery hotkeys plugin uses bind

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.

Upvotes: 2

Views: 1493

Answers (2)

Jonathan de M.
Jonathan de M.

Reputation: 9828

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

try

$(document).on('keyup', null, 'alt+a', function(){}

Upvotes: 4

Ohgodwhy
Ohgodwhy

Reputation: 50808

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

Upvotes: 0

Related Questions