0

When the user presses Enter button, the focus is being moved to next textbox. This is achieved in IE. But I planned to support multiple browser. I have changed most of the places and which seems to be working fine except one place, where I am assigning tab value to the window.event.keyCode.

I am assinging window.event.keyCode = 9 for IE.

To support multiple browser, I changed it to event.which = 9 and also tried with event.which.keyCode = 9, which is not at all working. Please help me find a way to focus the next textbox with unknown ID when I press enter.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Perseus
  • 1,546
  • 4
  • 30
  • 55
  • 1) PASS the event, 2) Choose the event `function (e) { var kc = e?e.which:event.keyCode; }` and you cannot force a tab since keyCode is read-only in most browsers. If you want to tab, use .focus() on the next field – mplungjan Apr 16 '13 at 04:41
  • `var key = e.keyCode || e.which;` – elclanrs Apr 16 '13 at 04:42

1 Answers1

2

Here is the cross browser method to get the keyCode

function (e) { var kc = e?e.which:event.keyCode; } 

you cannot force a tab since keyCode is read-only in most browsers. If you want to tab, use .focus() on the next field

To find the next field you will need to iterate the fields in the form.

Here are jQuery and plain JavaScript anwers:

enter key press behaves like a tab in javascript

Community
  • 1
  • 1
mplungjan
  • 169,008
  • 28
  • 173
  • 236
  • Thank you. As you said, keyCode is read-only in most browser, I don't know how come it is working in IE(may be supported). And this line changes the enter into tab. So I tried the same behavior in other browser using event.which as well as event.which.keyCode. I don't know why it is not working – Perseus Apr 16 '13 at 05:47
  • Guess, we should use only the focus() which will be the right solution – Perseus Apr 16 '13 at 05:48
  • But the problem is , there is a generic JavaScript written for all the pages. So I am able to get the keydown events but I don't have the object or ID to focus next – Perseus Apr 16 '13 at 05:52