Getting started

  1. How KO works and what benefits it brings
  2. Downloading and installing

Observables

  1. Creating view models with observables
  2. Working with observable arrays

Computed observables

  1. Using computed observables
  2. Writable computed observables
  3. How dependency tracking works
  4. Pure computed observables
  5. Reference

Bindings

Controlling text and appearance

  1. The visible and hidden bindings
  2. The text binding
  3. The html binding
  4. The class and css bindings
  5. The style binding
  6. The attr binding

Control flow

  1. The foreach binding
  2. The if and ifnot bindings
  3. The with and using bindings
  4. The let binding
  5. The component binding
  6. Binding lifecycle events

Working with form fields

  1. The click binding
  2. The event binding
  3. The submit binding
  4. The enable and disable bindings
  5. The value binding
  6. The textInput binding
  7. The hasFocus binding
  8. The checked binding
  9. The options binding
  10. The selectedOptions binding
  11. The uniqueName binding

Rendering templates

  1. The template binding

Binding syntax

  1. The data-bind syntax
  2. The binding context

Creating custom bindings

  1. Creating custom bindings
  2. Controlling descendant bindings
  3. Supporting virtual elements
  4. Custom disposal logic
  5. Preprocessing: Extending the binding syntax

Components

  1. Overview: What components and custom elements offer
  2. Defining and registering components
  3. The component binding
  4. Using custom elements
  5. Advanced: Custom component loaders

Further techniques

  1. Loading and saving JSON data
  2. Extending observables
  3. Deferred updates
  4. Rate-limiting observables
  5. Unobtrusive event handling
  6. Using fn to add custom functions
  7. Microtasks
  8. Asynchronous error handling

Plugins

  1. The mapping plugin

More information

  1. Browser support
  2. Getting help
  3. Links to tutorials & examples
  4. Usage with AMD using RequireJs (Asynchronous Module Definition)

The "class" and "css" bindings

Purpose

The class and css bindings add or remove one or more named CSS classes to the associated DOM element. This is useful, for example, to highlight some value in red if it becomes negative.

(Note: If you don’t want to apply a CSS class but instead want to assign a style attribute value directly, see the style binding.)

“class” binding example

<div data-bind="class: profitStatus">
   Profit Information
</div>

<script type="text/javascript">
    var viewModel = {
        currentProfit: ko.observable(150000)
    };

    // Evalutes to a positive value, so initially we apply the "profitPositive" class
    viewModel.profitStatus = ko.pureComputed(function() {
        return this.currentProfit() < 0 ? "profitWarning" : "profitPositive";
    }, viewModel);

    // Causes the "profitPositive" class to be removed and "profitWarning" class to be added
    viewModel.currentProfit(-50);
</script>

This will apply the CSS class profitPositive when the currentProfit value is positive; otherwise it will apply the profitWarning CSS class.

“css” binding example

<div data-bind="css: { profitWarning: currentProfit() < 0 }">
   Profit Information
</div>

<script type="text/javascript">
    var viewModel = {
        currentProfit: ko.observable(150000) // Positive value, so initially we don't apply the "profitWarning" class
    };
    viewModel.currentProfit(-50); // Causes the "profitWarning" class to be applied
</script>

This will apply the CSS class profitWarning whenever the currentProfit value dips below zero, and remove that class whenever it goes above zero.

“class” parameters

  • Main parameter

    The parameter value should be a string that corresponds to the CSS class or classes that you want to add to the element. If the parameter references an observable value, the binding will update the classes whenever the value changes, removing any previously added classes and adding the class or classes from new value.

    As usual, you can use arbitrary JavaScript expressions or functions as parameter values. Knockout will evaluate them and use the resulting value to determine the appropriate CSS classes to add or remove.

  • Additional parameters

    • None

“css” parameters

  • Main parameter

    You should pass a JavaScript object in which the property names are your CSS classes and their values evaluate to true or false according to whether the class should currently be applied.

    You can set multiple CSS classes at once. For example, if your view model has a property called isSevere,

    <div data-bind="css: { profitWarning: currentProfit() < 0, majorHighlight: isSevere }">
    

    You can even set multiple CSS classes based on the same condition by wrapping the names in quotes like:

    <div data-bind="css: { profitWarning: currentProfit() < 0, 'major highlight': isSevere }">
    

    Logically, 'major highlight': isSevere is equivalent to major: isSevere, highlight: isSevere. It’s merely a shortcut syntax if you want two or more CSS classes to be set and unset together.

    Non-boolean values are interpreted loosely as boolean. For example, 0 and null are treated as false, whereas 21 and non-null objects are treated as true. If your parameter references an observable value, the binding will add or remove the CSS class whenever the observable value changes. If the parameter doesn’t reference an observable value, it will only add or remove the class once and will not do so again later. As usual, you can use arbitrary JavaScript expressions or functions as parameter values. Knockout will evaluate them and use the resulting values to determine the appropriate CSS classes to add or remove.

    For backward compatibility, you can also use the css binding with a string value like the class binding.

  • Additional parameters

    • None

Note: Using the “class” and “css” bindings at the same time

As long as they reference different CSS class names, you can include both class and css bindings on the same element. Thus you can have some classes that are set based on a true/false value and others that are calculated dynamically. For example:

<div data-bind="css: { highlight: isSelected }, class: profitStatus">...</div>

If you want to apply the CSS class my-class, you can’t write this:

<div data-bind="css: { my-class: someValue }">...</div>

… because my-class isn’t a legal identifier name at that point. The solution is simple: just wrap the identifier name in quotes so that it becomes a string literal, which is legal in a JavaScript object literal. For example,

<div data-bind="css: { 'my-class': someValue }">...</div>

Dependencies

None, other than the core Knockout library.