Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
34
rated 0 times [  38] [ 4]  / answers: 1 / hits: 75112  / 12 Years ago, sat, march 3, 2012, 12:00:00

When adding a change event binding to an input box using knockout.js the old value is passed to the change function when the event is fired. I can work around this by using blur. Is this the intended behavior? Is the idea to use the change event to have the old value and then use a normal selector to get the value from the dom? It seems counter intuitive.



jsFiddle Example



JavaScript
----------
var data = {
saved_value:1,
value_changed: function(data){
alert(data.saved_value());
}
};
var viewModel = ko.mapping.fromJS(data);
ko.applyBindings(viewModel);

HTML
----
Current Value:<span data-bind=text:saved_value></span><br/>
<input data-bind=event:{change:value_changed},value:saved_value></input>

More From » knockout.js

 Answers
2

Try using the text and value bindings:



Current Value:<span data-bind=text: saved_value></span><br/>
<input data-bind=value: saved_value></input>


And change the JavaScript to this:



var data = {
saved_value: 1
};

var viewModel = ko.mapping.fromJS(data);
ko.applyBindings(viewModel);​


Here's an associated jsFiddle: http://jsfiddle.net/6zmJs/



If you want to alert() the value of saved_value when it is updated you could use ko.computed or viewModel.saved_value.subscribe(function(value) { alert(value); }); -- although those aren't the only ways to do this.


[#87073] Thursday, March 1, 2012, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jennie

Total Points: 593
Total Questions: 102
Total Answers: 106

Location: Federated States of Micronesia
Member since Fri, Sep 16, 2022
2 Years ago
;