Martin wrote:
>>Any suggestions as to how I can make this work?
>>
>>Thanks.
>>
>>function checkIt(fldID,maxValue,btnID){
>>if (document.getElementById(fldID).value > maxValue)
>>{alert('Invalid Lane Number');
>>document.getElementById(btnID).disabled=true;
>>document.getElementById(fldID).focus;}
>>else
>>document.getElementById(btnID).disabled=false;
>>}
>
>
> After clearing up the syntax error as noted by Michael & Randy, this
> function still did not work - or so I thought. Some further messing
> around with it revealed that apparently the focus IS being set to the
> input field (fldID) but then (I'm guessing) the TabKey keystroke
> executes and moves the cursor to the next field!
>
> I found that, if I mouse away from the field then the cursor will go
> back to the field and stay there. But keying away (with either the tab
> or Enter keys) results in the focus ending up somewhere else. (in
> fact, hitting <Enter> clicks an enabled button on the form AFTER
> closing the alert box!)
>
> I also found that, if I call this function from the onblur event
> (instead of onchange), it works perfectly. No matter which key is used
> the focus goes to the specified field and stays there. Apparently,
> onchange and onblur occur at different points relative to the
> keystroke itself.
>
> Since I don't want to use onblur, my question now becomes: how can I
> (in the function script) cancel the keystroke that occurred. (I'm
> coming from a Visual basic perspective here - in VB cancelling a
> keystroke is trivial). Is this possible in Javascript?
Instead of trying to cancel the keystroke, put a setTimeout call on your
focus(). That way, it has time to move to the next field, then the
script will set focus where you want it.
Instead of:
document.getElementById(fldID).focus();
Use something like this:
var myVar = null;
And then inside your function, set myVar = to the fieldID:
myVar = fldID;
And then:
myVar = setTimeout(setFocus,1000)
And then:
function setFocus(){
document.getElementById(fldID).focus();
}
--
Randy
comp.lang.javascript FAQ -
http://jibbering.com/faq