0

So I've got this javascript:

<script type="text/javascript">
    function OnKeyPress() {
        var charCode = window.event.keyCode;
        if (charCode == 13) {                
            window.event.keyCode = 9;
        }
    } document.onkeypress = OnKeyPress;
</script>

The idea is to catch an enter key press, and switch it to tab key press. And it half works - it catches the enter key. But it doesn't make it register as a tab key. I've tried using other keycodes as well (18 for alt) to confirm I wasn't just not seeing the tab happen.

Can anyone see what the problem is? Working in ASP.NET fwiw.

Thanks!

Karl
  • 189
  • 2
  • 3
  • 16

2 Answers2

0

You can't change the keyCode and have it trigger that event instead. It's just a captured value at that point. You might be able to obtain the effect by calling a function that simulates the desired key press event and canceling the current event instead.

Matt Pileggi
  • 7,126
  • 4
  • 16
  • 18
  • so you're suggesting dropping this bit 'window.event.keyCode = 9; ' and replacing it with a function call? Can you recommend a function that would produce the desired result? – Karl Feb 27 '14 at 19:44
  • Depends on what the desired results are. There's actually a similar question here: http://stackoverflow.com/questions/17482036/how-to-simulate-tab-key-with-enter-key-on-javascript Is that what you are trying to do also? – Matt Pileggi Feb 27 '14 at 19:46
0

You'll have to create the event handler for it.

Take a look:

How to trigger event in JavaScript?

Community
  • 1
  • 1
Renato Galvones
  • 533
  • 1
  • 3
  • 14
  • If you're in a hurry you can use jQuery to do it. Use the .trigger function. It will be a lot simpler, but doing it in pure javascript is far better from the learning perspective. ;) – Renato Galvones Feb 27 '14 at 19:41