|
|
|||||||||
|
|||||||||
|
|||||||||
| |
||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
JavaScript focus problem
I thought this would be really simple, but i'm stumped. I'd like to validate some data without submitting the form, but i can't seem to get the focus to go back to the field with the bad data. I've stripped out most of the code just to illustrate the problem. In both IE and Mozilla, the focus still goes to the next element. This is .php generated.
<html><body><script type="text/javascript"> var numAdults=5; var numChildren=0; function Check(element){ value=parseInt(element.value); if(isNaN(value) || value<0 || value>99){ if(element.name=='txtNumAdults'){ element.value=numAdults; }else{ element.value=numChildren; } element.focus(); return false; }else{ //SetDirty(element); return true; } } </script> <form action="a1.php" method="POST" name="myFrm"> <table> <tr><td align="right"><label>Adults:</label></td> <td><input type="text" name="txtNumAdults" size="3" value="5" onChange="Check(this);" /></td></tr> <tr><td align="right"><label>Children:</label></td> <td><input type="text" name="txtNumChildren" size="3" value="0" onChange="Check(this);" /></td></tr> </table></form></body></html> |
|
#2
|
|||
|
|||
|
what if you try being explicit in giving focus? something like this:
Code:
if (element.name=='txtNumAdults') {
element.value=numAdults;
myFrm.txtNumAdults.focus();
}
else {
element.value=numChildren;
myFrm.txtNumChildren.focus();
}
return false;
|
|
#3
|
|||
|
|||
|
Thanks, Haffej, but that didn't work either. We finally figured out it was a timing problem. The focus was going to the field but only for a brief instant. We put this in the code and now it works fine for all the browsers we've tested:
function fieldFocus() { element.focus(); } setTimeout( fieldFocus , 100 ); |
![]() |
| Viewing: ASP Free Forums > Programming > HTML, JavaScript And CSS Help > JavaScript focus problem |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|