Bindings in KnockoutJS - Part I

In this article series, we will discuss the bindings available in KnockoutJS. In Part I, we will see the overview of how to control the appearance & style of the controls using KnockoutJS Bindings.
 
To control the appearance and style of a control, KnockoutJS provides half a dozen binding handlers as in the following:
  1. visible binding
  2. text binding
  3. HTML binding
  4. CSS binding
  5. style binding
  6. attr binding
Let us overview each of the bindings.
 
The visible binding

The visible binding makes the binding element visible or hidden in the page. KnockoutJS changes the value of an element. Style.displays attributes based on the value supplied to the binding. The visible binding accepts the values like the boolean value false/true, or the numeric value 0, or null, or undefined.
  1. <b>visible binding</b>  
  2. <p>  
  3.     Enter your name: <input type="text" data-bind="value: name, valueUpdate: 'keyup'" />  
  4.     <p data-bind="visible: name().length > 0">Hello <span data-bind="text: name"></span></p>  
  5. </p> 
In the preceding HTML, we have a TextBox. When you start typing into the TextBox, we are making the Hello label visible using the visible binding. As shown in this example above, we are assigning the expression to the visible binding.

The text binding

The text binding displays the value supplied to it. KnockoutJS sets the supplied value to the innerText attribute of the associated element. The text binding accepts the numeric, string, observable & object. If the supplied value is other than observable then numeric & string, KnockoutJS converts it to value.toString and displays it.

  1. <b>text binding</b>  
  2.     <p>Hey <span data-bind="text: name"></span></p>  
  3. </p>
In the preceding example, Knockout displays the value from the name observable. Note: if you supply HTML markups as input then the text binding will display the HTML text as it is. It won't render the HTML text.
 
The HTML binding

The HTML binding renders the HTML markups as it is. The KnockoutJS sets the HTML markups to the elements innerHTML attribute. The following code displays the name with the "h1" text.
  1. <b>HTML binding</b>  
  2.   
  3. <p>Hi <span data-bind="html: nameHTML"></span></p> 
The viewmodel for the preceding view is as given below.

nameHTML = ko.observable("<h1> Jagan </h1>");

The CSS binding

The CSS binding applies or removes the CSS classes to/from the associated element.  For example, if you want to highlight the TextBox with the green color when the user entered the positive integer & highlight it in the red color when the input is negative.

CSS classes used:

  1. .greenbackgroundgreen; }  
  2. .red { backgroundred; }
Html Markup
  1. <b>CSS binding</b>  
  2.   
  3. <p> Enter integer: <input data-bind="value: integer, valueUpdate: 'keyup', css: integer() >= 0 ? 'green' : 'red'" /> </p> 
In the preceding markup, KnockoutJS will add the red class when the user types negative values into the TextBox, green class for positive values. The values for CSS can be:
  1. css: integer  
  2. css: { red: integer() < 0 }  
  3. css: { red: integer() < 0'green-highlight': isInteger } 
The style binding

The style binding sets the style attributes of the associates. We can use the same CSS binding example without CSS claases for style binding.
  1. <b>style binding</b>  
  2.   
  3. <p> Enter integer: <input data-bind="value: integerStyle, valueUpdate: 'keyup', style: { background: integerStyle() >= 0 ? 'green' : 'red' }" />  
  4.   
  5. </p> 
In the preceding example, we are setting the background of the associated element based on the expression.
The attr binding

The attr binding sets the associated elements attribute value. For example, if you want to set the title, href, id, ..... values of an element then a good way is to use an attr binding.
  1. <b>attr binding</b>  
  2.   
  3. lt;p><span data-bind="attr: { title: 'Hello ' + name() }">Hover here</span> </p> 
As mentioned in the preceding example, if you hover on the "Hover here" text then the input from the name text box's value will be displayed as a title since we are using an attr binding for the title attribute.

Soooo, we had a quick overview on the bindings for controlling the appearance & style.

Result: http://jsfiddle.net/Jaganathan/TXYG8/3/embedded/result,html,js,css

Reference: KnockoutJS


Similar Articles