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?
You can qualify the name using '
Like this:
Your Fiddle, updated
Here are the Knockout docs explaining the css binding: http://knockoutjs.com/documentation/css-binding.html
Just put it in quotes:
Here's an updated fiddle.
As a side note, you don't need the
> 0
since alength
of0
will evaluate tofalse
, and any other length will evaluate totrue
: