• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

How do I return focus to previous field

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
We are creating a time entry screen, so lots of text fields.
We want to edit the value entered, so are trying onBlur or onChange events. If an error is found (text field1), we want to set the focus on the field (text field1) in error. The problem is when we do that, it sets focus on the next text field in line (text field2), not the one in error.
How can we set the focus on the field they tabbed off of?
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Check the tab index of all your boxes and organize them in a cronological order from top to bottom then when setting focus to that specific one it will go to the next index in the hirearchi.
 
Kerry Shannon
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm sorry, my question amy not have been clear.
How do I set focus on the field I just changed?
We have the tab order set up and it works.
Our problem is: We do the onBlur or onChange, call a function, and in the function find an error. We have an alert, then set focus. When we test, the error is found so the call of the function and the function itself works. But, the focus is not on the changed field. It is on the next field in tab order.
 
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Kerry
Depending on what your method call looks like you can do a couple of things:
if you are passing the actual filed to the function you can use this to set the focus back on the field, something like this:
in the html
<input type=text onblur='verify(this);'>
then the function would look like this:
function verify(item){
// check the value
// if good just return, else
alert("The data you entered is wrong. Please fix it.");
item.select();
item.focus();
If your using a generic verification function then when you loop through the items on the page and find the right one you can just use its name to set the focus on it.
document.formName.fieldName.focus();
hope that helps
Dave
 
Kerry Shannon
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Something is not working. We can trace the error thru the function, but it does not return focus to the field in error. It goes on to the next field.
We are calling the function in an HTML onChange event:
onChange="checkTime(StartTime<%=x%> )"

Here's the javascript function:
function checkTime(timeObj)
{
alert("In Function")
fieldlength=timeObj.value.length
if (fieldlength == 0)
{
timeObj.select()
timeObj.focus()
}
if (fieldlength < 7)
{
alert(fieldlength)
alert("Invalid format! Try this format-- hh:mm AM or hh:mm PM")
timeObj.select(this)
timeObj.focus(this)
return false
}
return true
}
 
He does not suffer fools gladly. But this tiny ad does:
New web page for Paul's Rocket Mass Heaters movies
https://coderanch.com/t/785239/web-page-Paul-Rocket-Mass
reply
    Bookmark Topic Watch Topic
  • New Topic