Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Object JavaScript - Knockout attribute Bindings
<!DOCTYPE html>
.redItem {
color: red;
.greenItem {
color: green;
<div data-bind="html: someHtml"></div>
<div>And a link <a data-bind="attr: { href: myHref }">#</a></div>
<div data-bind="css: profitStatus, text: currentProfit"></div>
<button data-bind="click: reviseProfit">Revise Profit</button>
<script src="Scripts/knockout-2.2.1.debug.js"></script>
var viewModel = function () {
this.someHtml = "Here is a <strong>bold</strong> text.";
this.myHref = "#";
this.currentProfit = ko.observable(150000);
this.profitStatus = ko.computed(function () {
return this.currentProfit() < 0 ? "redItem" : "greenItem";
}, this);
this.reviseProfit = function () {
ko.applyBindings(new viewModel());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment