Click here to Skip to main content
15,881,809 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a text box with knockout binding as follows.
HTML
<input style="width: 385px; float: right" type="text" id="SymptomBox" placeholder="Symptoms" data-bind ="value : acceptedSymptom,valueUpdate:['afterkeydown'],event:{keypress:AddSymptoms}"/>


My view model is as follows
JavaScript
var ViewModel = function (data) {
var self=this;
self.acceptedSymptom=ko.observable();
}
ko.applyBindings(ViewModel);

I can update the observable array "acceptedSymptom" each time i press a key with key board.Clearly it is due to valueUpdate:['afterkeydown'] attribute binding to my input text box.This works fine,but i also want observable array to update value while i change value of input text box other than keydown.For example,i have a JQuery autocomplete plugin integrated to above mentioned text box.While i select any menu item from drop down by mouse click,observablearray should have changed value with change in value of input text box.It is noted that if i am selecting drop down values,using keyboard it works fine.Only issue is with selection with mouse pointer do not update observable array.Other than 'afterkeydown', i have also tried valueUpdate:['input','afterkeydown','change','blur','onLoad']. None of these make any sense.Please help me....
Posted

1 solution

Dear Jitendra,

Actually if you are using knockoutJS, this means that your design is centered around the model. Thus, if your autocomplete returns some value - most logically is to change value of the model, rather than it's presentation in input.

If you still insist on changing value of the input, just force triggering "change" event after the operation:

JavaScript
document.getElementById("changeit").addEventListener("click", function(){
    document.getElementById("SymptomBox").value = "new value";
    $("#SymptomBox").trigger("change");
});


Simple example:


http://jsfiddle.net/voronenko/QATTg/[^]
 
Share this answer
 
Comments
Jitendra Sabat 1-Aug-13 1:32am    
Thanks a load Vyacheslav.'trigger' event of jquery solved my problem. :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900