pistacchio pistacchio - 1 month ago 4x
Javascript Question

Knockout.js: Set a default text if a biding has a given value

I'd like to reset the value of an input field (

) whenever model.id is null.

How to bind the input value to respond to a certain value of an observable object? Something that would look like:

<input type="text" data-bind="text: if (model.value == null) { '' }" />


You can use ? operator in data-bind attribute:

<input type="text" data-bind="value: model.id() == null ? 'Default Value' : model.value()" />