I am trying to do the following functionality. Show a number of characters left in the textarea and disallow entering anything else if you already exceeded the maximum number.
I am confused with how to achieve this. Now I have something like this:
<textarea data-bind="textInput: message"></textarea>
<p>Characters left : <span data-bind="text: charLeft"></span></p>
function Vm_app() {
var self = this;
this.message = ko.observable('');
this.charLeft = ko.pureComputed(function(){
return 128 - self.message().length;
});
}
ko.applyBindings(new Vm_app());
Any idea how should I proceed?
P.S. I know how to achieve the task with listening for events, but I do not want to break MVVM paradigm.
P.S.2 the linked answer does not allow to continue adding the text, once you disabled it. I want only to disallow writing new characters (a person will be able to click delete, backspace).