[jQuery] Re: set focus on change

2008-02-04 Thread Scott Trudeau
Well, in that specific code, you should be able to just
$('#testField').focus(); because you are only working with a single field.

That said, I think maybe $(this).focus() is what you want for the generic
case? (can't recall what $(this) is inside an event callback).

Scott

On Feb 4, 2008 10:26 AM, Matt Williams [EMAIL PROTECTED] wrote:


 I want to run a validation function after a user enters data. If the
 validation fails, I want do an alert telling them so and set the focus
 back to that field. But even at the most basic level, I can't get the
 focus to stay / go back to the field that was changed. Interesting
 that it does seem to work in IE 7, but not FF.

 $(document).ready(function(){
  $('#testField').change(function() { alert('wrong value');
 this.focus(); });
 });

 Any ideas here? Or suggestions on how to reset focus back to the field
 that changed?

 Thanks,

 Matt




-- 
--
Scott Trudeau
scott.trudeau AT gmail DOT com
http://sstrudeau.com/
AIM: sodthestreets


[jQuery] Re: set focus on change

2008-02-04 Thread Matt Williams

I should clarify a bit more. The real implementation will be on
multiple fields and is for date validation / correction (i.e., changes
dashes to slashes).
$('#myDateField').change(function() { $.DateCorrection(this) });

If the date errors in this function, I have (function takes el as
parameter and sets error flag as it goes...)

if(error) {
alert(An invalid date was provided.);
el.value = '';
el.focus();
return false;
}

So this works in IE, but not FF. Same as the basic example. The
following works in IE, but not FF (forgot to add return false in orig.
post):
$('#testField').change(function() { alert('wrong value'); $
(this).focus(); return false;});

Matt

On Feb 4, 9:35 am, Scott Trudeau [EMAIL PROTECTED] wrote:
 Well, in that specific code, you should be able to just
 $('#testField').focus(); because you are only working with a single field.

 That said, I think maybe $(this).focus() is what you want for the generic
 case? (can't recall what $(this) is inside an event callback).

 Scott

 On Feb 4, 2008 10:26 AM, Matt Williams [EMAIL PROTECTED] wrote:





  I want to run a validation function after a user enters data. If the
  validation fails, I want do an alert telling them so and set the focus
  back to that field. But even at the most basic level, I can't get the
  focus to stay / go back to the field that was changed. Interesting
  that it does seem to work in IE 7, but not FF.

  $(document).ready(function(){
   $('#testField').change(function() { alert('wrong value');
  this.focus(); });
  });

  Any ideas here? Or suggestions on how to reset focus back to the field
  that changed?

  Thanks,

  Matt

 --
 --
 Scott Trudeau
 scott.trudeau AT gmail DOT comhttp://sstrudeau.com/
 AIM: sodthestreets


[jQuery] Re: set focus on change

2008-02-04 Thread Matt Williams

Just to follow up and hopefully help anyone else with this problem.
The issue is not jQuery or FF really, but just how javascript is
handled. Basically onBlur happens after the code is executed, meaning
that after this.focus(), it is still blurred.
So the solution is this:
setTimeout(function () { obj.focus() }, 50);

Googling this phrase led me to find this solution: javascript focus
after onchange

Matt