I have a drop down box including 'dog', 'cat', 'bear'. I want to show an input box in front of the select box when the user select 'cat' or 'dog'. So I am using the visible binding to do this:
<select data-bind="options:animals, value: animal"</select>
<input data-bind="value: description, visible: showDescription"/>
self.showDescription=ko.observable(false);
self.showOtherDescription = function() {
if(animal == 'cat' || animal == 'dog'){
self.showDescription=ko.observable(true);
}
}
It is working when the page is loaded. But when I change the option to 'bear' from the drop down it does not hide the input box. Does anybody have any idea?
The
showDescription
observable really should be the computed function to drive the visibility of the<input>
element on the view:I think you are assigning to the observable instead of using observable function invocation in the "showOtherDescription" function , try this
This is the way to go IMO:
It changes / adds a few things to your code:
showDescription
is not needed, at least not in the context of the things you've posted;showOtherDescription
needs to be acomputed
so it's updated when dependencies are updated;showOtherDescription
should not have a "setter" function or side-effects that change other observables. If you do need such a setter, check out writeable computed observables;animal
as a two-way bound property, it needs to be anobservable
and thus needs to be invoked as a function to get its value.animal
likeanimal == 'cat'
as well as use it in the View. I think it should probably be exposed publicly on the ViewModel, and as such you should refer to it asself.animal
.