9

I know this exact question was asked here, but the answer didn't work for what I needed to do so I figured I'd give some example code and explain a bit...

$(document).keypress(
    function (event) {
        // Pressing Up or Right: Advance to next video
        if (event.keyCode == 40 || event.keyCode == 39) {
            event.preventDefault();
            $(".current").next().click();
        }
        // Pressing Down or Left: Back to previous video
        else if (event.keyCode == 38 || event.keyCode == 37) {
            event.preventDefault();
            $(".current").prev().click();
        }
     }
 );

It basically disables the arrow keys to use them for something else, but doing:

$(document).keypress(function () { });

doesn't enable the default function again... I need it to scroll the page without having to create a scroll function for it...

Any ideas?

Thanks,
Matt

Community
  • 1
  • 1
Matt
  • 5,547
  • 23
  • 82
  • 121
  • right... sry I wrapped the "here" in an a tag but forgot to include the link. I've done so now, thanks for pointing that out :) – Matt Apr 09 '10 at 15:42
  • LOL, you know, when I read it I *thought* the phrasing looked like it was begging for a link... – T.J. Crowder Apr 09 '10 at 16:01
  • possible duplicate of [How to reenable event.preventDefault?](http://stackoverflow.com/questions/1164132/how-to-reenable-event-preventdefault) – Paŭlo Ebermann Nov 19 '11 at 18:59

5 Answers5

6

Adding a new handler doesn't replace the previous one, it adds a new one. You may be looking for jQuery#unbind if you're trying to remove the previous handler, but if you're going to be turning this on and off a lot, you probably would be better off with a flag telling you whether to prevent the default or not in your existing handler.

Adding, and later removing, a handler looks like this:

function keypressHandler() { /* ... */};

$('#thingy').keypress(keypressHandler);

// ...elsewhere...
$('#thingy').unbind('keypress', keypressHandler);
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • Your answer seemed to be a little closer to the proper way to do it, but I couldn't get it to work and danp's worked right away. – Matt Apr 09 '10 at 16:53
  • @Matt: Glad it got sorted. Actually, danp's answer is the same as the earlier one by Diodeus, it's just a *lot* more thoroughly described (which counts for a lot!). :-) – T.J. Crowder Apr 09 '10 at 16:59
3

I'm not sure this is the right way to handle it.

A better way to approach this problem would be to put some kind of check inside your document.keypress instructions.. like..

var enableKeys = false;

$(document).keypress(
    function (event) {
        // Pressing Up or Right: Advance to next video
        if (event.keyCode == 40 || event.keyCode == 39 && enableKeys) {
            event.preventDefault();

            $(".current").next().click();
        }
        // Pressing Down or Left: Back to previous video
        else if (event.keyCode == 38 || event.keyCode == 37 && enableKeys) {
            event.preventDefault();
            $(".current").prev().click();
        }
     }
 );

Then control the enablekeys wherever you feel necessary, either with a hover, or something along those lines.

danp
  • 14,876
  • 6
  • 42
  • 48
  • +1 I am not sure if this is "a better way", but it is definitely a viable alternative if, for whatever reason, [T.J. Crowder's solution](http://stackoverflow.com/questions/2608714/how-to-re-enable-default-after-doing-event-preventdefault/2608740#2608740) is unusable in some application. – Jørn Schou-Rode Apr 09 '10 at 15:49
  • Worked right away, guess it helped that it matched up exactly to my code. Thanks! – Matt Apr 09 '10 at 16:55
1
function(e){ e.preventDefault(); }

and its opposite

function(e){ return true; }
Joost
  • 3,169
  • 2
  • 22
  • 40
foxybagga
  • 4,184
  • 2
  • 34
  • 31
0

Why not just wrap a condition around event.preventDefault(); in your current code?

Diodeus - James MacFarlane
  • 112,730
  • 33
  • 157
  • 176
0

Try to unbind the keypress event from document.

I don't know of any other ways to do it.

HTH

Raja
  • 3,608
  • 3
  • 28
  • 39