HTML, JavaScript And CSS Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
 
User Name:
Password:
Remember me
Go Back   ASP Free ForumsProgrammingHTML, JavaScript And CSS Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread ASP Free Forums Sponsor:
  #1  
Old January 15th, 2004, 03:00 PM
CharlesTheHawk CharlesTheHawk is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Posts: 2 CharlesTheHawk User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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>

Reply With Quote
  #2  
Old January 26th, 2004, 11:29 AM
haffej haffej is offline
Contributing User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Posts: 87 haffej User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 5
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;

Reply With Quote
  #3  
Old January 26th, 2004, 10:00 PM
CharlesTheHawk CharlesTheHawk is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Posts: 2 CharlesTheHawk User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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 );

Reply With Quote
Reply

Viewing: ASP Free ForumsProgrammingHTML, JavaScript And CSS Help > JavaScript focus problem


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway