87

I have an input that has a knockout binding to the value.

When I update the value using jquery's .val() method, the changed value is not reflected in the viewModel.

I need to use jQuery to set this value. How can I trigger the update to the viewModel?

nnpath
  • 873
  • 1
  • 6
  • 4

1 Answers1

146

.val() does not trigger the change event. So, you can just do .val("blah").change() for KO to pick up the changes.

Josh Crozier
  • 233,099
  • 56
  • 391
  • 304
RP Niemeyer
  • 114,592
  • 18
  • 291
  • 211
  • 3
    What if you don't have control on that `val()`? Is there a way for jQuery to listen to `val()` on elements? – cherouvim Feb 14 '13 at 15:52
  • 1
    But really letting knockout do all the dom manipulation is a better approach, imho. – Tom Nov 14 '14 at 12:51
  • Make sure you check you `data-bind` statement too. I had this bug before I realized it should be `data-bind="value:location"` and *NOT* `data-bind="value:location()"` – jmbmage Jan 30 '18 at 15:24