Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

KnockoutJS - Updating ViewModel OnChange of textbox value instead of OnBlur Options

Tags:

knockout.js

I'm pretty new to KnockoutJS and I love what I've seen so far. Currently, when an observable property of the View Model is bound to the text property of the text box (input type=text), the ViewModel gets updated only on the blur event of the textbox. Is there a way to update the View Model on the change event of the textbox? I tried creating a custom binding handler on wiring up the change event handler on the text box in the "init", but it somehow did not work. Is this the correct to achieve this goal? Or is there an easier way?

like image 483
Scorpion-Prince Avatar asked Mar 07 '12 16:03

Scorpion-Prince


3 Answers

You can also use a 'value' binding and add the valueUpdate binding attribute to specify when to update your control:

See here: http://knockoutjs.com/documentation/value-binding.html

<p>Your value: <input data-bind="value: someValue, valueUpdate: 'afterkeydown'" /></p>
<p>You have typed: <span data-bind="text: someValue"></span></p> <!-- updates in real-time     -->

<script type="text/javascript">
var viewModel = {
    someValue: ko.observable("edit me")
};
</script>
like image 194
KodeKreachor Avatar answered Oct 14 '22 11:10

KodeKreachor


Above is not work while copy paste from mouse so you need to pass events in valueUpdate. like..

<p>Your value: <input data-bind="value: someValue, valueUpdate:['afterkeydown','propertychange','input']" /></p>

Try here http://jsfiddle.net/uJCQq/4/

like image 41
Nilesh Moradiya Avatar answered Oct 14 '22 10:10

Nilesh Moradiya


According to the official Knockout.js documentation:

Getting value updates instantly from inputs: If you are trying to bind an or to get instant updates to your viewmodel, use the textInput binding. It has better support for browser edge cases than any combination of valueUpdate options.

like image 1
dario Avatar answered Oct 14 '22 10:10

dario