This approach creates a computed observation that refers to the logical observable to determine which text will be displayed.
This is where jsfiddle works. http://jsfiddle.net/yq5rS/10/
And here is a brief description of the code
Html
<div class='liveExample'> <p> <label> <input type='checkbox' data-bind='checked: display' /> Active? </label> </p> <p data-bind='fadeVisible: IsActive()'></p> </div>
Scenarios
var Model = function() { var self = this; self.display= ko.observable(false); self.IsActive = ko.computed(function() { if (self.display()) return "Active." return "Not active." }); }; ko.bindingHandlers.fadeVisible = { init: function(element, valueAccessor) { var value = valueAccessor(); $(element).hide().html(ko.utils.unwrapObservable(value)).fadeIn(); }, update: function(element, valueAccessor) { var value = valueAccessor(); $(element).hide().html(ko.utils.unwrapObservable(value)).fadeIn(); } }; ko.applyBindings(new Model ());
EDIT
My original answer did not disappear, wait, and then fades out again. Here is the updated fadeVisible binding handler
ko.bindingHandlers.fadeVisible = { update: function(element, valueAccessor) { var value = valueAccessor(); $(element).fadeOut('slow', function () { $(element).html(ko.utils.unwrapObservable(value)).fadeIn(); }); } };
source share