I have a bit of javascript:
function ViewModel() {
var self = this;
self.highlight = ko.observable(true);
}
ko.applyBindings(new ViewModel());
And the html that complements it:
<div data-bind="css: { highlighted: highlight }, click: highlight( !highlight() )">
random string
</div>
What I'm trying to achieve:
- The css class 'highlighted' to only be activated when var highlight is true
- Clicking on the div will toggle the bool value of var highlight
- Wanted result: clicking on the div to activate/deactivate its css class
What I'm getting:
- The initial value of highlight is
true
, yet the css class starts deactivated (if I change the initial value tofalse
, the css class is activated: which seems as if I've somehow triggered the click bind when I haven't yet clicked anything) - The div's css class does not toggle on click
I'd rather not make a new corresponding click function inside ViewModel. I'm looking if possible for a bit of code I can place solely inline within the data-bind.
Here's the code on JSFiddle: http://jsfiddle.net/4wt4x/1/
Can anyone explain what's happening and what I'm doing incorrectly?