Reputation: 658
I have two input tags and I want the exact same value to be in both when I type in one. I am experimenting with knockout and am wondering how I would accomplish this.
<input type="text" data-bind="value: theValue" />
<br />
<input type="text" data-bind="value: theValue" />
<script type="text/javascript">
var viewModel = {
theValue: ko.observable("defaultText"),
};
ko.applyBindings(new ViewModel());
</script>
Upvotes: 1
Views: 1769
Reputation: 83358
What you have is correct, you just want to add valueUpdate: 'afterkeydown'
to your bindings so they update as you type.
<input type="text" data-bind="value: theValue, valueUpdate: 'afterkeydown'" />
<br />
<input type="text" data-bind="value: theValue, valueUpdate: 'afterkeydown'" />
Also, ko.applyBindings(new ViewModel());
should be ko.applyBindings(viewModel);
Here's a full demo
Upvotes: 1