views:

971

answers:

5

I have a form like this:

<form name="mine">
<input type=text name=one>
<input type=text name=two>
<input type=text name=three>
</form>

When user types a value in 'one', I sometimes want to skip the field 'two', depending on what he typed. For example, if user types '123' and uses Tab to move to next field, I want to skip it and go to field three.

I tried to use OnBlur and OnEnter, without success. Try 1:

<form name="mine">
<input type=text name=one onBlur="if (document.mine.one.value='123') document.three.focus();>
<input type=text name=two>
<input type=text name=three>
</form>

Try 2:

<form name="mine">
<input type=text name=one>
<input type=text name=two onEnter="if (document.mine.one.value='123') document.three.focus();>
<input type=text name=three>
</form>

but none of these works. Looks like the browser doesn't allow you to mess with focus while the focus is changing.

BTW, all this tried with Firefox on Linux.

+2  A: 

Try to attach tabindex attribute to your elements and then programmaticaly (in javaScript change it):

<INPUT tabindex="3" type="submit" name="submit">
dimarzionist
+1  A: 

You could use the onfocus event on field two, which will be called when it receives focus. At that point, field 1's value should be updated and you can perform your check then.

17 of 26
+1  A: 

If you used the method you describe, and they worked, the focus would also change when the user clicks on the field, instead of tabbing to it. I can guarantee you that this would result in a frustrated user. (Why exactly it doesn't work is beyond me.)

Instead, as said before, change the tabindex of the appropriate fields as soon as the content of field one changes.

Thomas
A: 
<form name="mine">
    <input type="text" name="one" onkeypress="if (mine.one.value == '123') mine.three.focus();" />
    <input type="text" name="two">
    <input type="text" name="three">
</form>
John Boker
A: 

Try onkeypress instead of onblur. Also, on the onfocus of field two is where you should be sending to three. I'm assuming you don't want them typing in two if one is 123 so you can just check that on two's onfocus and send on to three.

Chuck