View Full Version : Problem with focus()

virginia savage
06-21-2002, 01:46 AM
My HTML has an input text field that calls a javascript function when the field contents change (onChange).

The javascript function validates the data and if the data is in error sets the focus to the field with the error and returns false.

In NN 6.2 after clicking the "OK" in the alert error message box the cursor stays on the field in error for about 3 seconds and then jumps to the next input text field. How do I keep the cursor on the field in error so the user can correct the data?

In IE 5.5 it works incorrectly also, the cursor jumps to the next field right away.

In NN 4.7 it works correctly, the cursor stays on the field in error.

Here is my code

<td align="left" valign="top"><font size="4"><input type="text" name="application" value="" size=2 maxlength=3 onChange="return checkApplicationCode(this.value, this.name)">

function checkApplicationCode (s, n) {

if ((s != "lv") && (s != "LV") &&
(s != "tc") && (s != "TC"))
alert(n + " must be LV or TC. Please correct.");
return false;

I appreciate any help you can give me. Thanks.

06-21-2002, 02:24 AM
Without examining the code in detail, I'm wondering if perhaps the two different text fields erroneously have the same name?

06-21-2002, 04:17 PM
I beleive this has to do with the way the onchange event "bubbles" in different browsers. I will be the first to admit that I am not strong at event bubbling functions so I would try a differen event handler. This test scenario worked out:

<input type="text" name="appCode" value="" size="2" maxlength="3" onkeyup="if(this.value.length==2)checkApplicationCode(this.value, this.name)" />
<input type="text" name="nextfield" value="" size="2" maxlength="3" />
function checkApplicationCode (s, n) {
if ((s.toLowerCase()!="lv") && (s.toLowerCase()!="tc")) {
alert(n + " must be LV or TC. Please correct.");

By the way, why would you have a maxlength of 3 if the input must be 2 chartcers long?

06-21-2002, 09:00 PM

your code appears correct. No reason the focus should switch on to another element.
Therefore, there must be some other section of your code which might interfere, try posting a few more snippets, little by little we can find the possible error