Created
September 30, 2015 18:07
-
-
Save zach2825/5e158942c41afa7df347 to your computer and use it in GitHub Desktop.
keypressAction.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;(function ( $, window, document, undefined ) { | |
var pluginName = "keypressAction", | |
defaults = {}; | |
// The actual plugin constructor | |
function keypressAction ( element, options ) { | |
this.element = element; | |
this.settings = $.extend( {}, defaults, options ); | |
this._defaults = defaults; | |
this._name = pluginName; | |
this.init(); | |
} | |
$.extend(keypressAction.prototype, { | |
bindKeyToRoute: function (key, route) { | |
Mousetrap.bind([key], function(e) { | |
window.location = route; | |
return false; | |
}); | |
}, | |
init: function () { | |
var self = this; | |
$.each(this.settings.actions, function( index, object ) { | |
self.bindKeyToRoute(object.key, object.route); | |
}); | |
} | |
}); | |
$.fn[ pluginName ] = function ( options ) { | |
this.each(function() { | |
if ( !$.data( this, "plugin_" + pluginName ) ) { | |
$.data( this, "plugin_" + pluginName, new keypressAction( this, options ) ); | |
} | |
}); | |
// chain jQuery functions | |
return this; | |
}; | |
})( jQuery, window, document ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
That's it! Thank you for sharing. Just a note, if your binding html elements you can use the accesskey attribute for buttons and inputs I think.
https://www.w3schools.com/tags/att_global_accesskey.asp