Please consider this jsfiddle. It contains something along these lines:
<textarea data-bind="value: comment, valueUpdate: 'afterkyedown'"></textarea>
<br/><br/>
<span data-bind="text: getCount, valueUpdate: ['afterkeydown','propertychange','input']"></span> characters???
And this JavaScript:
var viewModel = function(){
var self = this;
self.count = ko.observable(0);
self.comment = ko.observable("");
self.getCount = function(){
var countNum = 10 - self.comment().length;
self.count(countNum);
};
}
var viewModel12 = new viewModel();
ko.applyBindings(viewModel);
I have a textarea where the maxlength should be 20 characters. When the number of characters reaches 20, it will be stop, and if you try to add more characters, they will be removed.
Note that this also has to work for copy/paste: if a user pastes more than 20 characters, only the first 20 will stay, and the rest of them should be removed.