3

i'm currently having problem in (maybe) a simple programming. i'm quite a newb in jquery

these are my codes:

$(':text').keyup(function(e) {
    if(e.keyCode == 13) {
        alert('Enter key was pressed.');

        $(this).trigger({type: 'keyup', keyCode: 9});
    }
});

i've searched almost 20 articles regarding this matter, none of them actually is as simple as this. and here's the problem

the tab key is supposed to replace enter, however, the tab did not trigger. and what i mean by not trigger, is kind of funny in some way. i've tried to change the 9 into 13 (which is the enter itself), and my browser will keep on looping the alert line endlessly, which means that, enter hits another enter, which is triggered by the code below the alert

what do i do wrong? do i have to tell the jquery what to do if keycode 9 is pressed? is there any way to make it trigger as if i hit the keyboard myself?

tyvm before :D any help will be greatly appreciated

krock
  • 28,904
  • 13
  • 79
  • 85
Alex
  • 31
  • 1
  • 2
  • Why would you be trying to replace the enter key specifically? Is something else bound to the enter key? Are you trying to make the form submit on tab? I do not see any evidence of the alert looping. See here: http://jsfiddle.net/philwinkle/6KdUj/ I really suggest using `console.log()` when doing debugging of this sort because alert blocks script execution; – philwinkle Mar 12 '11 at 05:49
  • because it's for moving between textboxes using enter instead of tab. and i think that having indexed textboxes is too much of a hassle, since all the textboxes are automatically generated (not code-wise, i coded it so that it will generate new set of textboxes should it run out of em) and another set of textboxes can be inserted in the middle, making the whole tab index thing to be pretty much worthless. and about the evidence of alert looping, try changing the keyCode:9 to keyCode:13. it WILL loop. – Alex Mar 12 '11 at 07:41

1 Answers1

0

Try resetting the focus rather than sending a tab.

$(':text').keydown(function(e) {
    if(e.keyCode == 13 && $(this).attr('type') != 'submit') {
        e.preventDefault();
        $(this).nextAll('input:first').focus();
    }
});
nhavens
  • 252
  • 1
  • 3
  • 11