...

View Full Version : Enable ENTER key instead of TAB key



chikna
05-14-2008, 08:20 PM
Following code works in IE but not in FF:



<body onkeydown="javascript:if(window.event.keyCode == 13) window.event.keyCode = 9;">


Modified for FF and still not working:



<script type="text/javascript">
function enterToTab(e) {
var intKey = window.Event ? e.which : e.KeyCode;

if(intKey == 13)
e.returnValue = false;
}
</script>

<body onkeydown="enterToTab(event);">


Please help!

Thanks.

mjlorbet
05-15-2008, 04:38 AM
e.preventDefault()


firefox way of killing the default behavior. opera tends to submit forms on enter regardless of catching for the key press.

chikna
05-15-2008, 01:34 PM
I changed code to:

if(intKey == 13) e.preventDefault();

Not working in FF.

Thanks.

mjlorbet
05-15-2008, 10:31 PM
<script type="text/javascript">
function enterToTab(e) {
var intKey = window.event ? event.keyCode: e.which;

if(intKey == 13){
e.cancelBubble = true;
e.preventDefault();
e.returnValue = false;
return false;
}
}
</script>


javascript is case sensitive and you logic in the switch based on window.event was backwards (moz doesn't support window.event, outside of as a value to pass into a function for use, not persisted, but it is the only one that supports the which property on event objects)

chikna
05-16-2008, 04:20 AM
mjlorbet, Thank you so much.

I tried your example and did not achieve anything.

For example,

<INPUT TYPE=TEXT NAME=A SIZE 20>
<INPUT TYPE=TEXT NAME=B SIZE 20>
<INPUT TYPE=TEXT NAME=C SIZE 20>

When I press enter in first input field, cursor stays in the same position in FireFox. However, if I press TAB, then cursor moves to next field.

How can I do this in FireFox. I was able to do it in IE (I hate though).

Thanks.

mjlorbet
05-18-2008, 03:29 AM
ah, i see what you're trying to do here now, looking at the big picture ;) you would need to use fireEvent or Invoke (i forget what the specific command is for it) in order to get the key press to fire, ie may let you assign the new keycode directly into the event object, but that's not the proper way to do things. the absolute easiest way to do this though is to create an array, the indicies would correspond to the element's position in the tab order and the value would be the id of the element that it was fired from



function arrayIndexOf(arr, str){
for(var i = 0; i < arr.length; i++)
if(str == arr[i])
return i;
return -1;
}



document.getElementById(arrayIndexOf((e?e.target:event.srcElement).id)+1).focus();


would be what you want to add to your event handler

mjlorbet
05-18-2008, 03:32 AM
oops i mistyped & the edit button isn't working



document.getElementById(tabOrder[arrayIndexOf(tabOrder, (e?e.target:event.srcElement).id)+1]).focus();


where tabOrder is the array you've created to contain all the elements that may be tabbed to in the order they should be tabbed to in



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum