0

I declare a textbox, dropdown list in knockout js. If I dynamically change the value of the textbox or dropdown using jQuery like this...

$('#IdNo').val(_IDNo);//for textbox
$('#IdNo').change();
$('#Subjects option').filter(function () { return $.trim($(this).val()) == parseInt(subjectbind); }).attr('selected', true);//for dropdown
$('#Subjects').change();

...then change() does not bind the value to the knockout. The changed value does appear in the UI but is not reflected in the View Model for further actions.

Jeroen
  • 60,696
  • 40
  • 206
  • 339
  • @supercool: i use the subscribe method as I get the dependent values. Problem is when i refresh I get the values changed in the UI as needed but It doesn't get bind to the knockout values. – Aarthi Ravendiran Aug 05 '15 at 06:37
  • it will work fine as expected check here http://jsfiddle.net/LkqTU/25697/ . let me know – super cool Aug 05 '15 at 06:38
  • @supercool: I can understand the fiddle. Is it possible to retain the changed value when the page is refreshed to the knockout.. – Aarthi Ravendiran Aug 05 '15 at 06:55
  • `page refresh` no we cant retain any value until unless we are storing somewhere i.e everything goes back to initial on refresh . – super cool Aug 05 '15 at 06:58
  • I store the values in the session. when i refresh i store the session values in variable and assign that to the textbox usnig the above code. When I do like this will it not bind to the knockout? Do i need to refer some other options? – Aarthi Ravendiran Aug 05 '15 at 07:06
  • if you are able to store in session from js you can definitely do it & it should work as how above fiddle works – super cool Aug 05 '15 at 07:10
  • I cudn't get the updated vlaue in the view model.. Any suggestion and idea to resolve this issue. – Aarthi Ravendiran Aug 05 '15 at 07:37
  • As far as I can tell I've answered *the question as you asked it*. If you have a different related question you should probably ask a new one, and *include all the code tor reproduce your scenario* (explain how it's different from this one), otherwise we can't really help you.... – Jeroen Aug 05 '15 at 07:58
  • @AarthiRavendiran before assigning check whether you have updated value session variable . you can mark `jeroen` answer and make a separate question what you now looking for . – super cool Aug 05 '15 at 08:30

1 Answers1

4

If you want to make sure Knockout takes note when you manually update the DOM, you need to use the trigger method like so:

$('#Subjects').trigger('change');

The change method can be used to register handlers for the event.

PS. If you're manually updating the DOM, then you should evaluate why / how you're using KnockoutJS...

Community
  • 1
  • 1
Jeroen
  • 60,696
  • 40
  • 206
  • 339