tags:

views:

167

answers:

3

I am working on an application in which I want to allow for quick data entry. So I want to make a date field selected by int month value (ie 7 = July). I think I only have 2 options.

  1. Prepend the number to the Month (1 -- Jan, 2 -- Feb, ... etc) but this is Ugly
  2. Use JS or jQuery to intercept key presses and select the appropriate month (doable, but messy)

Is there a way to have a hidden locater for a drop down?

-- Edit for Clarity --

I am trying to allow users to input the date through the keyboard more easily so instead of using a calendar picker (which I have) they could type .... 1 tab 1 tab 2010, which would result in the date Jan 1st, 2010

A: 

There is an accesskey attribute in HTML, which allows specifying to allow access using [Alt] + Character. But I don't know, whether this works for <option>:

<select>
    <option accesskey="1">January</option>
    <!-- ... -->
</select>
nikic
From the HTML specs: "The following elements support the accesskey attribute: A, AREA, BUTTON, INPUT, LABEL, and LEGEND, and TEXTAREA." But regardless, this would not work for Oct-Dec nor if there were multiple date boxes on the same page.
konforce
@konforce: [The HTML5 Standard](http://www.whatwg.org/specs/web-apps/current-work/multipage/editing.html#the-accesskey-attribute) allows this attribute on other elements, too, that's not the problem. The question is whether browser implement this for dropdowns ;) But yes, you're right, obviously this will fail with multiple month selects on one page.
nikic
I've just tried it: Firefox 4 doesn't support this.
nikic
+1  A: 

Here's an example of how to get it working:

$(function()
{
    $.each($("select.month"), function() { new MonthSelector(this); });
});

var MonthSelector = function(that)
{
    var self = this;
    $(that).bind("keypress", function(event) { self.onKeyPress(event); } );
    this.two = false;
    this.select = that;
};

MonthSelector.prototype.onKeyPress = function(event)
{
    if (event.which < 48 || event.which > 57) return;
    var digit = event.which - 48;

    if (this.two && digit <= 2)
    {
        digit += 10;
        this.two = false;
    }
    else
    {
        this.two = (digit == 1);
    }

    if (digit != 0) 
        this.select.selectedIndex = digit - 1; 
};

It's hard coded to work with 1-12 for simplicity's sake. (The date and year drop downs should automatically work by virtue of the browser's built in functionality.)

This also shouldn't affect the user's ability to (e.g.) type "Dec" for December.

konforce
A: 

You can do it with javascript easily. Just check for a selected drop-down box (onfocus) and keypress, if 1 is pressed then select the first element, 2 - second and so on. May also need to add some delay and wait for a second keypress in case of multiple digit numbers.

Alternatively you can try something not standard:

<label for="state">State:</label>
<select id="state" name="state">
    <option accesskey="1">.1.</option>
    <option accesskey="2">.2.</option>
    <option accesskey="3">.3.</option>
    <option accesskey="4">.4.</option>
</select>

By pressing Alt+Number you will be able to select elements in the drop-down. Just add accesskey attribute to the options dynamically (JavaScript) to your focused drop-down box.

negative
Why is this "not standard"?
nikic
-1 And I really do not appreciate it if my answer get's stolen. I will revert my vote is you roll back your answer to its original form.
nikic
It is not standard because it is not in w3 standard (duh!) and it is not guaranteed to work in different web browsers. Got stolen? How old are you, 10? It is not a race, I am pretty sure all comments were posted around the same time, so I didn't even see your reply when I was typing.
negative
By the way, the following elements support the accesskey attribute: A, AREA, BUTTON, INPUT, LABEL, and LEGEND, and TEXTAREA.
negative