Link to home
Start Free TrialLog in
Avatar of curiouswebster
curiouswebsterFlag for United States of America

asked on

Setting the focus with jQuery UI Date Picker

I have the validation almost working and need to return the focus to the date control, after the user clicks OK on the error messagebox.. When the user types an invalid date into the text field I display an error message saying "You have entered an invalid date". This works okay. But after clicking the OK button, I'd like to put focus back onto that control with the faulty date.

How can I do this?

Thanks!
Avatar of leakim971
leakim971
Flag of Guadeloupe image

           onClose: function (dateText, inst) {
                debugger;
              if (dateText != $.datepicker.formatDate($.datepicker.regional[''].dateFormat, new Date(dateText)))
              {
                  alert("The date you specified is not a valid date.");
                  this.value = '';
                  $(this).focus();
              }
Avatar of curiouswebster

ASKER

The focus does not return but I do see FireBug open up on the call to debugger. Cool.

remove << debugger; >>, line two, it should work
I took that out but it stilll does not work. We have some third party library (I think) that turns the currently selected field yellow. I think that's getting the focus and not giving it to the field properly.
try : setTimeout(function() { $("#datepicker").focus(); }, 250);
instead : $(this).focus();
Where do I locate that function?
It does not work.

I located it here:

    $(function () {
        $("#datepicker").datepicker({
                 // code removed for brevity
        });
        setTimeout(function () {
             $("#datepicker").focus();
        }, 250);

    });
>Where do I locate that function?

inside onClose, check my first post
ASKER CERTIFIED SOLUTION
Avatar of leakim971
leakim971
Flag of Guadeloupe image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Yes, that worked!