I know that you can bind some code to run when an input changes using the subscribe function, and I know that you can define what triggers the change event using the valueUpdate data binding. However, is there a way to bind one piece of code to run on say the 'change' type of valueUpdate, and different code to run on say either of the 'afterkeydown', 'input', or 'paste' types?
0
votes
1 Answers
0
votes
Absolutely. The "event" binding in knockout allows you to bind handlers to multiple events like this:
<input type="text" data-bind="event: {keypress: onKeyPress, change: onChange}" />
Keep in mind though, that Knockout will automatically stop event bubbling by default. In the example above, the onKeyPress handler will stop the keypress event from being handled by the browser, which will cause the change event to not fire. If you are binding multiple events, and you need your event handlers not to stop default event handling by the browser, just return "true" from your event handler.
You can see this code in action here : http://jsfiddle.net/rrahlf/QEuQR/