Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New Coder
    Join Date
    Nov 2007
    Posts
    71
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Enable ENTER key instead of TAB key

    Following code works in IE but not in FF:

    Code:
    <body onkeydown="javascript:if(window.event.keyCode == 13) window.event.keyCode = 9;">
    Modified for FF and still not working:

    Code:
    <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.

  • #2
    Regular Coder mjlorbet's Avatar
    Join Date
    Jan 2008
    Location
    Milwaukee, WI
    Posts
    724
    Thanks
    8
    Thanked 96 Times in 95 Posts

    e.preventDefault()


    firefox way of killing the default behavior. opera tends to submit forms on enter regardless of catching for the key press.
    -Mike
    "Want me to precludify him, like some kind of dispatcherator?... Can do!" -Bender

  • Users who have thanked mjlorbet for this post:

    chikna (05-16-2008)

  • #3
    New Coder
    Join Date
    Nov 2007
    Posts
    71
    Thanks
    9
    Thanked 0 Times in 0 Posts
    I changed code to:

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

    Not working in FF.

    Thanks.

  • #4
    Regular Coder mjlorbet's Avatar
    Join Date
    Jan 2008
    Location
    Milwaukee, WI
    Posts
    724
    Thanks
    8
    Thanked 96 Times in 95 Posts
    Code:
     
    <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)
    Last edited by mjlorbet; 05-15-2008 at 10:33 PM.
    -Mike
    "Want me to precludify him, like some kind of dispatcherator?... Can do!" -Bender

  • Users who have thanked mjlorbet for this post:

    chikna (05-16-2008)

  • #5
    New Coder
    Join Date
    Nov 2007
    Posts
    71
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Help!

    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.

  • #6
    Regular Coder mjlorbet's Avatar
    Join Date
    Jan 2008
    Location
    Milwaukee, WI
    Posts
    724
    Thanks
    8
    Thanked 96 Times in 95 Posts
    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

    Code:
     
    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
    -Mike
    "Want me to precludify him, like some kind of dispatcherator?... Can do!" -Bender

  • Users who have thanked mjlorbet for this post:

    chikna (05-19-2008)

  • #7
    Regular Coder mjlorbet's Avatar
    Join Date
    Jan 2008
    Location
    Milwaukee, WI
    Posts
    724
    Thanks
    8
    Thanked 96 Times in 95 Posts
    oops i mistyped & the edit button isn't working

    Code:
     
    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
    -Mike
    "Want me to precludify him, like some kind of dispatcherator?... Can do!" -Bender

  • Users who have thanked mjlorbet for this post:

    chikna (05-19-2008)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •