Chapter 2. The Ins and Outs of Data Binding
Binding HTML Data
In Example 1-1, text was data-bound to a
span tag. Like text, it’s quite common to data-bind content that contains HTML data.
In Example 2-1, the
getName function is expanded to wrap the output of the
name data binding with some HTML code.
HTML Versus Text Binding
HTML bindings work similarly to text with the exception that they render any HTML tags provided. If you were to use the
text binding with HTML content, the HTML would be escaped, and the content would be displayed as text.
In Example 2-1, I update two things. First, the
data-bind attribute is moved from the
span tag to the
h1 tag. Second, it is bound to the
getName function, which has been updated to contain some HTML code.
Example 2-1. h1 tag with HTML binding
<title>Data Binding with KnockoutJS
Binding HTML Attributes, CSS Classes, and CSS Styles
Knockout can bind almost ...