About data binding by knockout js and value change


Thomas

There are two textboxes and a span bound by culling. The example I got is easy to understand. Here is the code.

<p>First name: <input data-bind="value: firstName" /></p>
<p>Last name: <input data-bind="value: lastName" /></p>
<h2>Hello, <span data-bind="text: fullName"> </span>!</h2>

var ViewModel = function(first, last) {
    this.firstName = ko.observable(first);
    this.lastName = ko.observable(last);

    this.fullName = ko.computed(function() {
        // Knockout tracks dependencies automatically. It knows that fullName depends on firstName and lastName, because these get called when evaluating fullName.
        return this.firstName() + " " + this.lastName();
    }, this);
};

ko.applyBindings(new ViewModel("Planet", "Earth")); 

When the code runs whatever value is passed, it is reflected, but when I change a textbox value, the changed data is not reflected immediately as it keyuphappens , but when the focus changes, the changed data is reflected in the span. So please guide me if i want to change the data on keyup then i need to change the code. thanks

Artem Vyshniakov

You should use bindings valueUpdatefor this :

<p>First name: <input data-bind="value: firstName, valueUpdate: 'keyup'" /></p>
<p>Last name: <input data-bind="value: lastName, valueUpdate: 'keyup'" /></p>
<h2>Hello, <span data-bind="text: fullName"> </span>!</h2>

Here is the link to the documentation : http://knockoutjs.com/documentation/value-binding.html

Related


About data binding by knockout js and value change

Thomas There are two textboxes and a span bound by culling. The example I got is easy to understand. Here is the code. <p>First name: <input data-bind="value: firstName" /></p> <p>Last name: <input data-bind="value: lastName" /></p> <h2>Hello, <span data-bind=

About data binding by knockout js and value change

Thomas There are two textboxes and a span bound by culling. The example I got is easy to understand. Here is the code. <p>First name: <input data-bind="value: firstName" /></p> <p>Last name: <input data-bind="value: lastName" /></p> <h2>Hello, <span data-bind=

Knockout.js data binding hidden field value

Veda Shurab I have a hidden field in a knockout template whose value is updated using jquery. The problem is that when trying to pass this value to the server using ajax, I get a null value in the controller. But the html source shows that the value of the hid

Knockout.js data binding hidden field value

Veda Shurab I have a hidden field in a knockout template whose value is updated using jquery. The problem is that when trying to pass this value to the server using ajax, I get a null value in the controller. But the html source shows that the value of the hid

Knockout.js data binding hidden field value

Veda Shurab I have a hidden field in a knockout template whose value is updated using jquery. The problem is that when trying to pass this value to the server using ajax, I get a null value in the controller. But the html source shows that the value of the hid

Ajax data binding with Knockout Js

Kitty Sarvaj I am using knockout js and I am finding it difficult to bind data in ajax get method, I have created model, viewModel and ajax function, I have ajax method in the same js file where I am creating viewModel on page load ajax and try to bind my html

Ajax data binding with Knockout Js

Kitty Sarvaj I am using knockout js and I am finding it difficult to bind data in ajax get method, I have created model, viewModel and ajax function, I have ajax method in the same js file where I am creating viewModel on page load ajax and try to bind my html

Knockout.js - "value" binding in "html" binding

thread lemon I'm working on an application that needs to generate HTML dynamically based on some values. I put the following code where the dynamic HTML is: <div data-bind="html: extraHTML"></div> I have an object setup in my javascript file that contains var

Knockout.js - "value" binding in "html" binding

thread lemon I'm working on an application that needs to generate HTML dynamically based on some values. I put the following code where the dynamic HTML is: <div data-bind="html: extraHTML"></div> I have an object setup in my javascript file that contains var

Modify value inside data binding in knockout

Srivisah I'm using knockout to develop a page. I have bound the array to html as follows data: {'options':[{'name':'hi'},{'name':'hello'}]) HTML: <div data-bind="foreach: options"> <div data-bind="text: name, css: name"></diV> </div> Each option

Knockout Js radio binding not setting value

Callum Linington Here's the documentation I'm looking at: Example of adding a radio button it says: And only if the parameter value is equal to the value attribute of the radio button node, KO will set the element to check What I did here: jsfiddle self.radioV

Knockout Js radio binding not setting value

Callum Linington Here's the documentation I'm looking at: Example of adding a radio button it says: And only if the parameter value is equal to the value attribute of the radio button node, KO will set the element to check What I did here: jsfiddle self.radioV

Knockout js: data binding click not working

Benjamin Baggins I can't trigger the knockout feature logMyStuffon my website with a simple button click . Note that I've added <button>, <a>and <div>tried to get it to work. I made a JS fiddle, but the JS fiddle worked. Here is my code: var SearchFilterViewMo

Knockout.js data binding to ViewModel

Damon I'm trying to figure out KnockOut data binding and am struggling to get a simple form to bind to a ViewModel. I am using WebAPI to extract my JSON data. This is my ViewModel, when this "find" method is called, it creates a new WorkOrder object and popula

Knockout.js data binding to ViewModel

Damon I'm trying to figure out KnockOut data binding and am struggling to get a simple form to bind to a ViewModel. I am using WebAPI to extract my JSON data. This is my ViewModel, when this "find" method is called, it creates a new WorkOrder object and popula

Does data binding show div knockout js

XAF So I'm trying to display some divs that basically show boxes of different colors based on their number, basically both AA and B return 5, so they should show, while other divs should be empty or empty, or even not show the divs at all, But for some reason

styled data binding not working in knockout js

Govan violin Style bindings don't work in knockout js. I don't know why it doesn't work? Code: <div class="sample" data-bind="style:{color:'blue'}"> hai </div> please help me. Damian You forgot to call ko.applyBindings(). see violin