36

I have a case where I want to capture the simultaneous key press event of "shift+tab" key using jquery. Actually as you all know it is used to move tab in backward direction but for me in one scenario tha tab was not working in any direction i.e. neither forward nor backward. so I found a jquery function for moving tab in forward direction as follows:-

$(':input').live('keydown', function(e) {    
    var keyCode = e.keyCode || e.which;     
    if (keyCode == 9) {    
        tindex = parseInt($(this).attr("tabindex")) + 1;
        if($(":input[tabindex='" + tindex + "']"))
        {
            $(":input[tabindex='" + tindex + "']").focus();
        }
    }
});

Now I want to move tah tab in backward direction as well. Can somebody guide me how can i achieve this???

John
  • 1
  • 13
  • 98
  • 177
Hitesh Gupta
  • 361
  • 1
  • 3
  • 3

1 Answers1

79

You can use e.shiftKey to check whether the shift key was held when the event was triggered.

If you add an if statement to your event handler, checking whether the shift key was held, you can perform different actions:

if(keyCode == 9) {
    if(e.shiftKey) {
       //Focus previous input
    }
    else {
       //Focus next input
    }
}
James Allardice
  • 164,175
  • 21
  • 332
  • 312