Connecting Tech Pros Worldwide Forums | Help | Site Map

javascript/ change enter key to tab keypress

Newbie
 
Join Date: Apr 2006
Posts: 2
#1: Apr 27 '06
this work great and stops all enter key presses. now how do i disable the event and fire a tab key event.
so if a user presses the enter key i need it to be ignore and tab to the next field.

<head>
<script type="text/javascript">

function kH(e) {
<!--
var pK = document.all? window.event.keyCode:e.which;
return pK != 13;
}

document.onkeypress = kH;
if (document.layers) document.captureEvents(Event.KEYPRESS);
file://-->
</script>
</HEAD>

Newbie
 
Join Date: Apr 2006
Posts: 2
#2: Apr 27 '06

re: javascript/ change enter key to tab keypress


here is code that works! must be in body tag.
<body> onkeydown="if (event.keyCode==13) {event.keyCode=9; return event.keyCode }"
Newbie
 
Join Date: Jun 2006
Posts: 1
#3: Jun 1 '06

re: javascript/ change enter key to tab keypress


Quote:

Originally Posted by rrosebro

here is code that works! must be in body tag.
<body> onkeydown="if (event.keyCode==13) {event.keyCode=9; return event.keyCode }"

The above code was a lot help. But in my web page I have to also access the links using keyboard. In that case I have to use the alt + access key and then press enter to access a particular link. So if I use the above code then I am not able to access the links using keyboard.
Newbie
 
Join Date: Feb 2007
Posts: 3
#4: Feb 15 '07

re: javascript/ change enter key to tab keypress


This Code is Working Fine with all controls , means the Focus is moving to Next Control. BUT this is not working When the Focus is in "HtmlInput File"Control which we will use for File UpLoadings. means Focus is not moving to Next Control. Then How to modify code.


Quote:

Originally Posted by rrosebro

this work great and stops all enter key presses. now how do i disable the event and fire a tab key event.
so if a user presses the enter key i need it to be ignore and tab to the next field.

<head>
<script type="text/javascript">

function kH(e) {
<!--
var pK = document.all? window.event.keyCode:e.which;
return pK != 13;
}

document.onkeypress = kH;
if (document.layers) document.captureEvents(Event.KEYPRESS);
file://-->
</script>
</HEAD>

asipo's Avatar
Newbie
 
Join Date: Jun 2008
Location: malaysia
Posts: 1
#5: Jun 26 '08

re: javascript/ change enter key to tab keypress


actually the code provide by rrosebro is not working for me =.=

and i try alot with that kind of coding where u try to set the 13 into 9

and i think the web browser ( im using mozilla firefox 2.0.0.14 ) not allowing us to change that

so heres my code to solve the enter as tab ( using an id as element )


at the script part
Expand|Select|Wrap|Line Numbers
  1.        function testing(event,number)
  2.     {
  3.         if( event.keyCode == 13 )
  4.         {
  5.             var wow = document.getElementById(number);
  6.             wow.focus();
  7.         }        
  8.     }

so the input will look like this
Expand|Select|Wrap|Line Numbers
  1. <form>
  2.           <input id="1" type="text" tabindex="1" onkeyup="testing(event,2)" />
  3.           <input id="2" type="text" tabindex="3" onkeyup="testing(event,3)" />
  4.           <input id="3" type="text" tabindex="2" onkeyup="testing(event,1)" />
  5. </form>
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#6: Jun 27 '08

re: javascript/ change enter key to tab keypress


Welcome to Bytes and thanks for sharing.

Remember that IDs should not start with or be only numbers, so use a string not starting with a number.
Newbie
 
Join Date: Jul 2008
Posts: 1
#7: Jul 17 '08

re: javascript/ change enter key to tab keypress


its too simple dear,
in body tag just add this
<body onkeydown="if (event.keyCode==13) {event.keyCode=9; return event.keyCode }">

DONE!.. :) Now your ENTER key will be treated as TAB key to focus other controls..

Regards
Sheryar Nizar
[url removed]
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#8: Jul 17 '08

re: javascript/ change enter key to tab keypress


Are you sure about that? Have you tested on browsers besides IE? What about textareas?

I've removed the link in your post. Please read the Posting Guidelines.
Familiar Sight
 
Join Date: Feb 2007
Posts: 207
#9: Jul 17 '08

re: javascript/ change enter key to tab keypress


Quote:

Originally Posted by rrosebro

this work great and stops all enter key presses. now how do i disable the event and fire a tab key event.
so if a user presses the enter key i need it to be ignore and tab to the next field.

Try using EnterToTab
Reply