Tab with Enter Key

Had a requirement in come to me recently in a web application for the ability to move from one field to the next using the [ENTER] key, rather than the [TAB] key.  This is a fairly common setup in a rapid 10-Key or Reverse 10-Key data entry.  The solutions I found via google weren't very clean.  One required the developer to include on onkeypress='' for every textbox, calling a function with the name of the next field to move to.  Sounded like a maintenance nightmare.  I could imaging getting all 50 fields setup then 1-week after deployment the customer asks to rearrange some fields. 

I put together something that worked for me.  Maybe it can work for you :)  The code has a generic keystroke handler routine that should be easily adaptable to any keystroke interception routines (like customizing the [PAGE UP] or arrow keys).  Enjoy

<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <title>Tab with Enter Key</title>
    <script type="text/javascript">      
      // xBroswer event subscription
      function addListener(a,b,c,d){if(a.addEventListener){a.addEventListener(b,c,d);return true;}else if(a.attachEvent){var e=a.attachEvent("on"+b,c);return e;}else{alert("Handler could not be attached");}}
      function bind(a,b,c,d){return window.addListener(a,b,function(){d.apply(c,arguments)});}
      //
 
      // Generic dispatcher for keystrokes
      function handleKeystroke(evt) 
      {              
        evt = getEvent(evt);
        var asc = getKeyCode(evt);
        var chr = getCharacter(asc);
 
        for (var i in this)
        {
          if (asc == i)
          {
            this[i](evt);
            break;
          }
        }
      }
      //
 
      // xBrowser event utilities
      function cancelEvent(evt)
      {
        evt.cancelBubble = true;
        evt.returnValue = false;
        if (evt.preventDefault) evt.preventDefault();
        if (evt.stopPropagation) evt.stopPropagation();
        return false;
      }
      function getEvent(evt)
      {
        if( !evt ) evt = window.event;
        return evt;
      }
      function getTarget(evt)
      {
        var target = evt.srcElement ? evt.srcElement : evt.target;
        return target;
      }
      function getKeyCode(evt)
      {
        var asc = !evt.keyCode ? (!evt.which ? evt.charCode : evt.which) : evt.keyCode;
        return asc;
      }
      function getCharacter(asc)
      {
        var chr = String.fromCharCode(asc).toLowerCase();
        return chr;
      }
      //
 
      function handleEnterKey(evt)
      {       
        var target = getTarget(evt);
        var targetTabIndex = target.tabIndex;
        var nextTabIndex = targetTabIndex+1;
 
        var nextElement = getElementByTabIndex(nextTabIndex);
        if( nextElement )
        {
          nextElement.focus();
          return cancelEvent(evt);
        }
 
        return true;
      }
 
      function getElementByTabIndex(tabIndex)
      {
        var form = document.forms[0];
        for( var i=0; i<form.elements.length; i++ )
        {
          var el = form.elements[i];
          if( el.tabIndex && el.tabIndex == tabIndex )
          {
            return el;
          }
        }
 
        return null;
      }
 
      // Setup our Key Commands
      var keyMap    = new Array();
      var ENTER     = 13 // ASCII code
      keyMap[ENTER] = handleEnterKey;
 
      // Add the keypress listner to the document object for global capture
      bind(document, 'keypress', keyMap, handleKeystroke);
    </script>
  </head>
  <body onload="document.forms[0].elements[0].focus();">
    <form id="form1" name="form1">
      Textbox One: <input type="text" id="txtOne" name="txtOne" tabindex="1" /><br />
      Textbox Two: <input type="text" id="txtTwo" name="txtTwo" tabindex="2" /><br />
      <input type="submit" value="Submit" />
    </form>
  </body>
</html>

posted @ Monday, October 09, 2006 6:25 AM


Print

Comments on this entry:

# re: Tab with Enter Key

Left by Willie at 10/12/2006 12:57 PM

Another idea to get around the maintenance nightmare would to just wrap it in a server control. This way, the script and textbox would be all wrapped up into the same tight package. If you needed to change something do it just once. The global capture looks nice, but I do wonder about the compatibility in browsers and the performance - although we're not on 266Mhz machines anymore so ...

# re: Tab with Enter Key

Left by Dexter at 5/19/2007 11:28 PM

I like it! I was looking for something like this and did exactly what Willie suggested by putting it into its own C# class and inherited from the control when needed. I needed my code to do additional stuff on postback (like pull data from SQL) but upon doing so, I didn't want it to return to the control that spawned the postback but to the next in tab order which this code solved.

Newsgroups make great think tanks. Thanks!!!

# re: Tab with Enter Key

Left by Naveen at 9/14/2007 1:30 AM

Very helpful

# Tab with Enter Key

Left by Sheryar Nizar at 7/17/2008 11:44 AM

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
http://www.sheryar.net

# re: Tab with Enter Key

Left by Adso da Melk at 7/25/2008 10:24 AM

cool Sheryar Nizar

but it is not working with FF

Your comment:



 (will not be displayed)


 
 
 
Please add 7 and 8 and type the answer here:
 

Live Comment Preview:

 
«August»
SunMonTueWedThuFriSat
272829303112
3456789
10111213141516
17181920212223
24252627282930
31123456