0

I am trying to select a month in dropdown using keyboard like pressing 1-january 2-february etc.. I tried to acheive this one using javascript...i got it upto september,what i have to add to acheive the full result ,this is my code...

JAVASCRIPT:

$(".select_month").on('keyup',function(){
    var index;
    if(event.keyCode>=96&&event.keyCode<=105) {
            index=((event.keyCode+1)-96)-1;
        document.getElementById("month").selectedIndex =index;
    }
});

select_month is class name for select ...Please Help me...THanks in advance

Ravi
  • 17
  • 1
  • 10
  • possible duplicate of [Set the selected index of a Dropdown using jQuery](http://stackoverflow.com/questions/1314245/set-the-selected-index-of-a-dropdown-using-jquery) – krzysiej Mar 16 '15 at 07:13

2 Answers2

0

First you could to this nativly by just adding "1 January", "2 February" etc - but I understand if it's too ugly.

Secondly, you forgot the "event" argument in the function. Without this event.keyCode could not exist. Then you would have to create a timeout function that waits for a second for another character to be entered, like first 1 and then 2, to reprecent december. If the user has pressed a keyUp within the last second, add it to the other keycode and do something.

Tony Gustafsson
  • 828
  • 1
  • 7
  • 18
-1

This is a very interessting question. So I tried out something, here is the fiddle

http://jsfiddle.net/2g53299u/

$(".select_month").on('keyup',function(event){
    if(event.keyCode>=48&&event.keyCode<=57) {
        var index;
        var fentry;
        if($(this).hasClass('firstkey')) {
            index = 10+event.keyCode-48;
        } else {
            $(this).addClass('firstkey');
            index = event.keyCode-48;
        }
        $(".select_month option[value=" + index +"]").addClass("selectedmonth");
        setTimeout(function() {
            $(".selectedmonth").prop('selected', true);
            $('.firstkey').removeClass('firstkey');
            $('.selectedmonth').removeClass('selectedmonth');
        }, 500); 
    }
});

I am certain, you could do it more elegant (without classes), but for the first shot, it works. The point is, I am using a timeout-function to receive more than 1 keyboard input.

Cheers R

rst
  • 2,510
  • 4
  • 21
  • 47