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 "hasFocus" binding

Purpose

The hasFocus binding links a DOM element’s focus state with a viewmodel property. It is a two-way binding, so:

  • If you set the viewmodel property to true or false, the associated element will become focused or unfocused.
  • If the user manually focuses or unfocuses the associated element, the viewmodel property will be set to true or false accordingly.

This is useful if you’re building sophisticated forms in which editable elements appear dynamically, and you would like to control where the user should start typing, or respond to the location of the caret.

Example 1: The basics

This example simply displays a message if the textbox currently has focus, and uses a button to show that you can trigger focus programmatically.

The textbox has focus

Source code: View

<input data-bind="hasFocus: isSelected" />
<button data-bind="click: setIsSelected">Focus programmatically</button>
<span data-bind="visible: isSelected">The textbox has focus</span>

Source code: View model

var viewModel = {
    isSelected: ko.observable(false),
    setIsSelected: function() { this.isSelected(true) }
};
ko.applyBindings(viewModel);

Example 2: Click-to-edit

Because the hasFocus binding works in both directions (setting the associated value focuses or unfocuses the element; focusing or unfocusing the element sets the associated value), it’s a convenient way to toggle an “edit” mode. In this example, the UI displays either a <span> or an <input> element depending on the model’s editing property. Unfocusing the <input> element sets editing to false, so the UI switches out of “edit” mode.

Name:  

Click the name to edit it; click elsewhere to apply changes.

Source code: View

<p>
	Name: 
	<b data-bind="visible: !editing(), text: name, click: edit">&nbsp;</b>
	<input data-bind="visible: editing, value: name, hasFocus: editing" />
</p>
<p><em>Click the name to edit it; click elsewhere to apply changes.</em></p>

Source code: View model

function PersonViewModel(name) {
    // Data
    this.name = ko.observable(name);
    this.editing = ko.observable(false);
        
    // Behaviors
    this.edit = function() { this.editing(true) }
}

ko.applyBindings(new PersonViewModel("Bert Bertington"));

Parameters

  • Main parameter

    Pass true (or some value that evaluates as true) to focus the associated element. Otherwise, the associated element will be unfocused.

    When the user manually focuses or unfocuses the element, your value will be set to true or false accordingly.

    If the value you supply is observable, the hasFocus binding will update the element’s focus state whenever that observable value changes.

  • Additional parameters

    • None

Dependencies

None, other than the core Knockout library.