Core jQuery event modification problem
Posted
by
DSKVR
on Stack Overflow
See other posts from Stack Overflow
or by DSKVR
Published on 2011-01-04T20:42:51Z
Indexed on
2011/01/04
20:53 UTC
Read the original article
Hit count: 292
I am attempting to overwrite a core jQuery event, in this case the keydown event. My intention is to preventDefault() functionality of Left(37), Up(38), Right(39) and Down(40) to maintain the consistency of hot keys in my web application.
I am using the solution provided here for the conditional charCode preventDefault problem.
For some reason, my function overwrite is simply not firing, and I cannot put my finger on the problem. I am afraid that over the past 30 minutes this issue has resulted in some hair loss. Anybody have the remedy?
/*
Modify Keydown Event to prevent default PageDown and PageUp functionality
*/
(function(){
var charCodes = new Array(37,38,39,40);
var original = jQuery.fn.keydown;
jQuery.fn.keydown = function(e){
var key=e.charCode ? e.charCode : e.keyCode ? e.keyCode : 0;
alert('now why is my keydown mod not firing?');
if($.inArray(key,charCodes))
{
alert('is one of them, do not scroll page');
e.preventDefault();
return false;
}
original.apply( this, arguments );
}
})();
© Stack Overflow or respective owner