0

I am trying to use jquery to override the default tabbing navigation. I cannot simply use the tabindex property because I am trying to get the tab key to navigate from a text input to a virtualized textbox (codemirror). I have been trying to use the following javascript/jquery to no avail:

$('#modelName').focus(function() { 
    $(this).keydown( function(event) {
        if(event.keyCode=='9') {
            codeMirror.focus();
        }
    });
});

Any thoughts on how to make this work?

Brian Peacock
  • 711
  • 8
  • 20

2 Answers2

1
  $('#modelName').keydown( function(event) {
    if(event.keyCode == 9) {
        event.preventDefault();
        codeMirror.focus();
    }else{
        alert("Not the right key! " + event.keyCode);   
    }
  });

It's good to have a catch, just so you can see where you're going wrong. In this case, I think it's string vs int.

Also, the way your code is, you would be applying a new keydown event handler each time the #modelName gets focus, without removing the old one. Would likely cause problems later.

ahren
  • 16,803
  • 5
  • 50
  • 70
0

try this

$('#modelName').keyup(function (e) {
    if(e.keyCode== 9){
    codeMirror.focus();
    }
});

use keyup() instead of keydown()

Valli69
  • 8,856
  • 4
  • 23
  • 29
  • `keydown` would feel more right if you were trying to mimic general tab behaviour. Also, `keyCode` 27 is the `esc` key... – ahren May 24 '12 at 05:19