Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is knockout.js textInput binding not working?

Tags:

knockout.js

Example taken from the documentation and stuck to a fiddle doesn't work. Code snippet for completeness:

<p>Login name: <input data-bind="textInput: userName" /></p>
<p>Password: <input type="password" data-bind="textInput: userPassword" /></p>

ViewModel:
<pre data-bind="text: ko.toJSON($root, null, 2)"></pre>

<script>
    ko.applyBindings({
        userName: ko.observable(""),        // Initially blank
        userPassword: ko.observable("abc")  // Prepopulate
    });
</script>

I've tried it in an incognito window thinking that some browser extension might be messing with it. No luck.

Expected behavior is that viewModels's JSON dump should be updated with each keystroke in any of the input fields change.

If I switch to value binding instead of textInput it does update whenever input focus changes.

Has anyone encountered this?

like image 313
uKolka Avatar asked Aug 27 '14 00:08

uKolka


2 Answers

If you are stuck on an older version of Knockout you can use valueUpdate

<input data-bind="value: firstName, valueUpdate:'afterkeydown'" />

https://stackoverflow.com/a/4391419/1058292

like image 99
atreeon Avatar answered Oct 22 '22 03:10

atreeon


The textInput binding was added in a later version of Knockout.JS (3.2.0).
Add the updated library to your fiddle and it works.

like image 32
Sarah Bailey Avatar answered Oct 22 '22 01:10

Sarah Bailey