views:

782

answers:

5

I am using JQuery ui with datepicker and when a user tabs on to a field, they appropriately get the calendar pop up.

  1. User tabs (via key onto field
  2. User selects date with mouse click
  3. User tabs
  4. Tabindex starts over at one (at the beginning of form)

Here is code. (May also have the tab index set)

<input type="text" name="demo" />
<input type="text" class="date" />

The jquery code is:

$(".date").datepicker();

Any suggestions on how I might go about solving this issue (bonus to shortest solution)?

A: 

You can probably use the onClose event to return focus to your input when the datepicker closes, as this refers to the associated input field in that callback. For example:

$('.date').datepicker({
   onClose: function() { this.focus(); }
});
Adam Bellaire
+1  A: 

Subscribe to the onClose or the onSelect event:

$("#someinput").datepicker(
{
    // other options goes here
    onSelect: function ()
    {
        // The "this" keyword refers to the input (in this case: #someinput)
        this.focus();
    }
});

Or in the case of onClose:

$("#someinput").datepicker(
{
    onClose: function ()
    {
        this.focus();
    }
});
roosteronacid
both of these work, however the calendar drops down again. Is there any quick solution around that?
Jeff Ancel
If I could get it to focus on the next field...onClose:function(){ focus on next field } (Would Work). Any suggestions?
Jeff Ancel
A: 

Having it focus on the same box ultimately causes the date picker to pop back up and what we really want is for it to go to the next field. Here is teh devised solution:

  1. Requires the tabindex attribute to be set on your fields.
  2. This is casting from a string to int in javascript (the * 1).
  3. This actually moves the selected field to the next element, instead of the current focus

    $(function(){
      $(".date").datepicker({
          onSelect: function(){
              currenttab = $(this).attr("tabindex");
              nexttab = currenttab * 1 + 1;
              $(":input").each(function(){
                  if ($(this).attr("tabindex") == nexttab)
                     $(this).focus();
              });
      }
      });
    });
    

Any suggestions to improve on this technique are appreciated.

Jeff Ancel
A: 

Hi,

Thanks for the suggestion Jeff, I modified your onSelect function to use the attribute filter to retrieve only elements with the tabindex specified by "nexttab".

$(function(){
  $(".date").datepicker({
      onSelect: function(){
          currenttab = $(el).attr("tabindex");
          nexttab = currenttab * 1 + 1;
          $("[tabindex='" + nexttab + "']").focus();
      }
  });
});

PS: If you don't have tabindexes specified in your code, like I neglected to do earlier, simply add the following code:

$('input, select').each(function(i, val){
    $(this).attr('tabindex', i + 1);
});
A: 

If you really don't care about the tab order, that is, not having it assigned so it follows the flow of the page, you could do something like this.

jQuery('.date').datepicker({
    'onSelect': function(){
        $(this).nextAll('input, button, textarea, a').filter(':first').focus();
    }
});

It works on the assumption that when the user selects the date, he is finished with that field and moves on to the next one, so it simply selects the next field.

Jimmy Stenke