2

What is the best way to get the selected item value from a dropdown list box when the user does one of the following;

hits the tab key on the item,

presses the enter key or

mouse clicks the item.

Do I have to create a javascript event handler for each event or is there a good way to do it with knockout.

Are there any good jsfiddle examples I could look at?

thanks

LRP
  • 283
  • 1
  • 3
  • 5

1 Answers1

1

You could use a custom binding that catches those events.

ko.bindingHandlers.tabEnterClick = {
    init: function(element, valueAccessor) {
        $(element).click(function() {
            valuAccessor()();
        }).keydown(function(event) {
            if (event.which == 13 /*enter*/ || event.which == 9 /*tab*/) {
                valuAccessor()();
            }
        }
    }
};

But if you just want to know the selected item from a dropdown, the value binding does that just fine.

Michael Best
  • 16,623
  • 1
  • 37
  • 70
  • thanks for your suggestion, gives me something to try. I'm having a hard time with the event binding, it works for tabbing off of an input field, but the click won't refresh all the other observables until another click is done. It's built on the jquery autocomplete which takes an input box and makes it into a drop down as you type. If I click anywhere else after clicking on the selection in or out of a field then everything refreshes like it does with a tab. – LRP Mar 07 '13 at 02:51