Take the 2-minute tour ×

I am trying to use the right arrow key to change between classes but I am having problems with triggering the event with the arrow key. Instead it functions on mouse click. The right arrow key keyCode and which is 39. I have tried both and get the same result.

    console.log(evt.keyCode);// used to verify the keyCode 39

    if (evt.keyCode == 39) {

        }, function() {

        }, function() {
        else {
        return false;
share|improve this question
Have you tried testing for evt.which instead of .keyCode? –  Lorax Dec 12 '12 at 15:19
Thanks for your response and yes I tried both and have used both but I get the same thing. –  user1898151 Dec 12 '12 at 23:47
add comment

1 Answer

up vote 1 down vote accepted

Edit --

Everything is correct, except the toggle. Here is what I assume you are trying to do -


$(function() {

    $(document).keyup(function(evt) {

        if (evt.keyCode == 39) {

            if ($('body').hasClass('narrow')) {
            } else if ($('body').hasClass('large')) {
            } else {



share|improve this answer
Thanks for your response. That works, thanks! Great, but I was under the impression that if there was no class named with .removeClass() that it would remove all classes. From api.jquery.com/removeClass: "If a class name is included as a parameter, then only that class will be removed from the set of matched elements. If no class names are specified in the parameter, all classes will be removed." Any ideas on where I went wrong with the original code? Thanks again. –  user1898151 Dec 12 '12 at 23:51
Yes, you are correct, but why remove all classes, when you know which ones to remove? If you ever needed to add a class to it for general styling, then you'd be out of luck at that point, since it would be removed also :) Just my two cents. **edit -- of course, you probably wouldn't run into that issue on the body, but just in general usage this is why I would not remove all classes. –  JSess Dec 12 '12 at 23:54
True. Unfortunately due to my lack of reputation I am unable to give you any, but thanks again. –  user1898151 Dec 12 '12 at 23:58
You just gave me rep by accepting this answer, so that is fine. :) Glad I could help! –  JSess Dec 13 '12 at 0:00
add comment

Your Answer


