odedbd
odedbd

Reputation: 2375

valueUpdate: 'afterkeydown' for input type="numeric" in Knockoutjs 2.0

[see fiddle for illustration]

I set up a value bind to an input of type number, and want the bound observable to immediately reflect changes to the value of the field. to do that I set the afterkeydown valueUpdate binding. This works well for changing the number input using the arrow up and arrow down keys. However, if I change the number using the browser-generated (tested in chrome) increment/decrement control the change is only reflected upon changing focus to a different element. I assume this reflects the default update upon change event.

My question is whether there is any way to set the update to occur for both changes using the up down keyboard errors and browser-generated up/down error controls?

Upvotes: 9

Views: 14069

Answers (1)

RP Niemeyer
RP Niemeyer

Reputation: 114792

The valueUpdate additional binding can take an array of events. It looks like the oninput event is fired when clicking on the up/down arrows.

So, you can bind it like:

<input type="number" data-bind="value: y, valueUpdate: ['afterkeydown', 'input']"/>

http://jsfiddle.net/rniemeyer/hY5T2/9/

Upvotes: 19

Related Questions