Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Why doesn't focus() method work 1

Status
Not open for further replies.

LyndonOHRC

Programmer
Sep 8, 2005
603
US
This function fires on the onchange event of a form/input element. For some reason the focus goes to the next element in the DOM.

I'm not sure if the onchange or onblur event is the best way to trigger this validation process. Or why I'm not getting the behavior I expected.

Any help appreciated.
Code:
if (isNaN(RSWinAwardValue) ||  Number(RSWinAwardValue)<=0) {
 alert('Award Amount Must be a Positive Number');
 document.getElementById('RSWinAward').focus();
 return ;
}

Lyndon

---People Remember about 10% of what you say ---They never forget how you made them feel. Covey
 
If I trigger the function with the onblur event (not my prefrence) the focus() method moves the cursor to the input tag that has invalid data (the desired behavior). But using the onchange event the cursor goes to the next element in the DOM???

Ideas?

Lyndon

---People Remember about 10% of what you say ---They never forget how you made them feel. Covey
 
I still need help with this issue. Maybe if I restate the problem.

It seems to comes down to me not understanding the behavior difference between onchange and onblur.

My goal is to put focus() on the <input> element I'm checking when the data entered is not valid.

If I trigger my validation function with the onchange event(my desired method) the focus method seems to be ignored and focus goes to the next element in the DOM.

If I trigger the exact same function using the onblur event (don't want to use that event) the focus() method seems to work and the cursor is in the element that failed the validation.

Any help appreciated.

Lyndon

---People Remember about 10% of what you say ---They never forget how you made them feel. Covey
 
Your relevant html script and javascript function, how do they look like? otherwise it is too philosophic.
 
The OnBlur event is triggered when the element loses focus, the OnChange event is triggered when the element changes.

You are right in wanting to use onChange and not onBlur, otherwise, every time you leave the textbox you would trigger the function.

Now I tested this out in FF and IE, and I could not make FF return focus to the textbox no matter what I tried.

For IE and only IE add a return false to the OnChange event to cancel the action of leaving the textbox.

onChange="return myfunction();";

And in your function return a false value when the input is not the expected one, and a true value when it is.

if (isNaN(RSWinAwardValue) || Number(RSWinAwardValue)<=0) {
alert('Award Amount Must be a Positive Number');
document.getElementById('RSWinAward').focus();
return [red]false[/red];
}
else{
return true;
}

FF for whatever reason will not cancel the action, and moves the focus to whatever was clicked or tabbed to.

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Phil, thanks so much for the umteenth time. :)

I knew of this when using the onsubmit event but never thought of it for onchange.

You sir, are a champ!

For those who asked following is code with Phil's solution in red.
Code:
<input name="RSWinAward" id="RSWinAward" size="10" maxlength="10" value="#Chart.RSWinAward#" onchange="[COLOR=red][b]return[/b][/color] CalcTotalAwardPaid('RSWinAward');">


function CalcTotalAwardPaid(elem) {
 if (elem='RSWinAward'){
 var RSWinAwardValue=document.getElementById('RSWinAward').value;
 if (isNaN(RSWinAwardValue)|| Number(RSWinAwardValue)<=0){
  document.getElementById('RSWinAward').value=RSWinAwardValue;
  alert('Award Amount Must be a Positive Number');
  document.getElementById('RSWinAward').focus();
  document.getElementById('RSWinAward').select();
  return [COLOR=red][b]false[/b][/color];
 } else {RSWinAwardValue=Math.round(RSWinAwardValue); 
  document.getElementById('RSWinAward').value=RSWinAwardValue;}
 }
}

Lyndon

---People Remember about 10% of what you say ---They never forget how you made them feel. Covey
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top