user2344496 user2344496 - 4 months ago 31
Javascript Question

Knockout JS valueUpdate keypress not working for enable binding

I have a 'Save' button on my form that has knockout enable binding. I added valueUpdate: 'keypress' to my knockout binding expression hoping for save button to become enabled on keypress. I have a simple input textbox with value knockout binding. Instead, page still requiring user to click away for save button to become enabled. HTML below. when user types in input, canSave becomes true enabling save button.

<button data-bind="click: save, enable: canSave, valueUpdate: 'keypress'">Save</button>

<h3><strong>First Name: <input data-bind="value: firstName"></strong></h3>

Answer

The valueUpdate binding only makes sense coupled with a value binding; it should go on your input, not on your button.

<button data-bind="click: save, enable: canSave">Save</button>
<h3><strong>First Name: <input data-bind="value: firstName, valueUpdate: 'keypress'"></strong></h3>

Also, consider valueUpdate: 'input' if you're using browsers new enough to support it, or even the textInput binding, if you're using KO >3.2