I have a data binding in Knockout to apply a CSS class if a condition is true. When I use a dash in the class name (such as test-class) then I get a javascript error.
Here is a fiddle that demonstrates the problem: http://jsfiddle.net/sgvem/2/
<p data-bind="text: property, css: { with-dash: property().length > 0 }"></p>
Is there a way to add a class with a dash using Knockout JS?
Just put it in quotes:
Here’s an updated fiddle.
As a side note, you don’t need the
> 0since alengthof0will evaluate tofalse, and any other length will evaluate totrue: