var ViewModel = function (message) {
this.message = ko.observable(message);
this.message.subscribe(function(newValue){
if(!this.couldSend())
this.message(newValue.substr(0,140));
}, this);
this.remaining = ko.computed(function () {
return 140 - this.message().length;
}, this);
this.couldSend = ko.computed(function () {
return this.message().length > 0 && this.message().length < 140;
}, this);
this.show = function () {
alert(this.message());
};
};
ko.applyBindings(new ViewModel(""));
<h1>What's happening?</h1>
<textarea cols='50' rows='3' data-bind='value: message, valueUpdate:"afterkeydown"'></textarea>
<div class="remaining" data-bind='text: remaining'></div>
<br/>
<button data-bind='enable: couldSend'>Send</button>
* {
font-family:Calibri;
margin: 5px
}
textarea {
border: 1px solid rgba(200, 200, 200, 1);
border-radius: 10px;
padding: 4px;
}
.remaining {
position: absolute;
top: 57px;
left: 341px;
opacity: .3;
font-weight: bold;
}
External resources loaded into this fiddle: