6

My view model is returning a true value, and I'm trying to get my template to add CSS accordingly. The problem is, I can't find the syntax for a != true.

I have something like this:

<div data-bind="css: {'lw-touched': checked, 'lw-touch': !checked}"></div>

Which I thought would say, apply 'lw-touched' if checked === true, apply 'lw-touch' if checked === false. But that doesn't work :(. So I tried this:

<div data-bind="css: {'lw-touched': checked, 'lw-touch': checked !== true}"></div>

Which also didn't work.

I'm sure there is a way to do this! I just can't find it at the moment.

3
  • Damn it...just posted and solved it 30 seconds later. I'm leaving it cause someone else might have this problem... data-bind="css: {'lw-touched': checked, 'lw-touch': checked() !== true}"> Commented Feb 16, 2012 at 22:30
  • Leave that as an answer to your question. Commented Feb 16, 2012 at 22:32
  • 4
    Actually you can use just !checked(). Whenever you are negating an expression you need to get the underlying value using the parenthesis. Commented Feb 16, 2012 at 22:39

3 Answers 3

20

I posted and solved it 30 seconds later :(.

I'm leaving it because someone else might have this same problem.

data-bind="css: {'lw-touched': checked, 'lw-touch': !checked()}">

Also I used the better syntax thanks to @MikaelÖstberg

I'm marking this as the answer so that I don't get more negative feedback :/

Sign up to request clarification or add additional context in comments.

1 Comment

Would be great if you highlight that the brackets () made the difference. Nevermind, now my comment serves the purpose! ;)
1
data-bind="css: isPlaying() ? 'play' : 'pause'"

Consider to add the () at the binding field.

1 Comment

after applying this i got something strange: class='0 1 2 3' in case of play value and class='0 1 2 3 4' for pause value. it seem like knockout took string.length value instead of string for class attribute. any idea ?
1

Thank you g.breeze, for your answer. I've been unsuccessfully trying to use the ternary operator on the css property, but never knew it could be set without the curly braces. One for the books!

data-bind="css: ifThisExpressionIsTrue() ? 'applyThisClass andAnother' : 'elseThisClass'"

You've bumped a two-year-old topic with your valuable answer.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.