Knockout If Binding

by Pratik Bhoir

HTML

<div data-bind="text: ko.toJSON(displayMessage)"></div>
<input data-bind="value: txt1" />
<div data-bind="if: displayMessage">This message is displayed on If condition.</div>

JavaScript

function viewModel() {
    var self = this;
    this.txt1 = ko.observable("");
    this.displayMessage = ko.computed(function () {
        console.log(self.txt1().toString().trim() == "");
        if (self.txt1().toString().trim() == "") return false;
        else return true;
    });
}
var ViewModel = new viewModel();
ko.applyBindings(ViewModel);