Sure it is possible with Javascript! You need to call the field.focus
() function upon detecting that the period key has been pressed. To
detect the Key pressed create a Key handler that is raised "onKeyUp"
in the textfield and then check the event.keyCode property. The event
is available via the window.event in IE and in Firefox, you will need
to explicitly pass a parameter for the event.

Here's a sample page I created to illustrate the process:

---
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://
www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml";>
<head>
  <title>Untitled Page</title>
  <script language="javascript" type="text/javascript">
    function JumpFocus(e, field)
    {
      // Get the field that received the keypress.
      var thisElement = field;
      // Get the event in all browsers.
      var evt = (window.event) ? window.event: e;

      // The keyCode of the "." (period) key is 190.
      if ((e.keyCode || evt.which) == 190)
      {
        // Period key was pressed. Shift the focus to the next
control.
        // Iterate through the form elements and find the next one in
tab order.
        var frm = document.forms["frm1"];
        for (var i = 0; i < frm.elements.length; i++)
        {
          var nextElement = frm.elements[i];
          if (nextElement.tabIndex == thisElement.tabIndex + 1)
          {
            nextElement.focus();
            // Focus doesn't work on FireFox, so call select() as
well.
            nextElement.select();
            break;
          }
        }
      }
    }
  </script>
</head>
<body>
  <form id="frm1">
    <input id="Text1" type="text" onkeyup="JumpFocus(event, this)"
tabindex="0" />&nbsp;
    <input id="Text2" type="text" onkeyup="JumpFocus(event, this)"
tabindex="1" />&nbsp;
    <input id="Text3" type="text" onkeyup="JumpFocus(event, this)"
tabindex="2" />&nbsp;
    <input id="Text4" type="text" onkeyup="JumpFocus(event, this)"
tabindex="3" />&nbsp;
  </form>
</body>
</html>
---

Note that you do not have to iterate through all the form elements if
you have a small collection of fields - In such as case, you could
hard code the next field name instead of relying on tab order. Another
way is to simulate a tab keypress but that is complicated to get right
(unless you use JQuery).

On Apr 20, 9:36 pm, IAK <[email protected]> wrote:
> Hi,
> I have to manage this thing:
> 4 textboxes for an IPAddress.
> The user ask me to change the focus when he presses "." (DOT)
> For example...
> The user is on the first textbox...
> He digits "123" and "." and the second textbox gets the focus...
> Is it possible to do it with a Javascript????
>
> Thanx
>
> Giacomo

Reply via email to