Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

knockout.js event that tracks every text change inside a input text box

I'm new to knockout js. I want to call a function every time a text changes inside a text box. I researched a bit and implemented keyup, keydown and keypress but they didn't work properly. If anybody could give me a solution or please redirect me to some document that is helpful for my scenario. And If there is some sort of documentation about all of the events (inbuilt and custom) that are available in knockout Js, that would be really helpful.

To be specific about the problem:

  data-bind="value: targetProp, event:{keyup: $parent.changeProp}"

And in Js:

    Inside parent:
     this.changeProp = function () {
                if (condition..) {
                       do something...
                }
            }
      

It's not working with key up. For simple solution, please give me something that will alert the length of the string that has been written inside a textbox (on every text entered and deleted).

like image 585
user79307 Avatar asked Jul 28 '13 06:07

user79307


People also ask

What is data bind in knockout JS?

Knockout's declarative binding system provides a concise and powerful way to link data to the UI. It's generally easy and obvious to bind to simple data properties or to use a single binding.

How do you get the knockout value from observable?

To read the observable's current value, just call the observable with no parameters. In this example, myViewModel. personName() will return 'Bob' , and myViewModel. personAge() will return 123 .

What is two way data binding in knockout JS?

KO is able to create a two-way binding if you use value to link a form element to an Observable property, so that the changes between them are exchanged among them. If you refer a simple property on ViewModel, KO will set the form element's initial state to property value.

How does KnockoutJS work?

KnockoutJS is basically a library written in JavaScript, based on MVVM pattern that helps developers build rich and responsive websites. The model separates the application's Model (stored data), View (UI) and View Model (JavaScript Representation of model).


1 Answers

You can use valueUpdate: 'afterkeydown' which updates your view model as soon as the user begins typing a character.

data-bind="value: targetProp, valueUpdate: 'afterkeydown'"
like image 163
david.s Avatar answered Nov 14 '22 20:11

david.s