Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

KnockoutJS, Text vs Value Binding. Why Text Binding Doesn't Bind to an Input Field?

From Knockout tutorial:

Why this works?

 <p>First name: <input data-bind="value: firstName" /></p>

While this doesn't?

 <p>First name: <input data-bind="text: firstName" /></p>

ViewModel

// This is a simple *viewmodel* - JavaScript that defines the data and behavior of your UI
 function AppViewModel() {
this.firstName = "Bert";
this.lastName = "Bertington";
}
like image 483
usefulBee Avatar asked Oct 18 '13 14:10

usefulBee


1 Answers

Because in html, the typed text in an 'input type="text"' is stored in an attribute named value.

The value binding affect the value attribute of the element and the text binding alters the inner text of an element.

By writing

<input data-bind="text: firstName" />

you are trying to change the content of the input element. And an input doesn't allow a content.

<input value="where the value binding writes its data">
    where the text binding writes its data
</input>
like image 60
Damien Avatar answered Nov 02 '22 08:11

Damien