USER_8675309
USER_8675309

Reputation: 893

knockout data-binding not working as expected with jquery

I have an element that is data bound like this:

<input data-bind="value: observableCity" class="form-control" id="cityInput" type="text">

And another element like this:

<span class="" data-bind="text: observableCity"></span>

and some js that does this on an ajax call

$('#input1').val(data.City);

When I enter text into the <input> it is updated in the <span> as well, however, when i insert a value to the <input> using jquery it does not update in the <span> as well. Is there a way to fix this that I missed?

Upvotes: 0

Views: 379

Answers (1)

Franco
Franco

Reputation: 12545

You don't have to set the input's value via jQuery since such value will not be assigned to your observable variable, that's why your span doesn't reflect the input's value you just entered.

The observable's values are set by calling them as follows: observableCity(data.City)

Upvotes: 2

Related Questions