35

Can someone tell me what's wrong with this http://jsfiddle.net/Yp8Bz/? It works fine in Chrome/Firefox, but in IE 8 I get the following error:

Message: Unable to parse bindings.
Message: SyntaxError: Expected identifier, string or number;
Bindings value: click: blah, attr: {class: 'Hi'}
Line: 38
Char: 359
Code: 0
URI: http://cdnjs.cloudflare.com/ajax/libs/knockout/2.0.0/knockout-min.js

2 Answers 2

67

Put class in quotes. I ran into the same issue when using a reserved word.

attr: {'class': 'Hi'}
6
  • 9
    This also applies to if. In case anybody finds this page with the same problem I had. Nov 2, 2012 at 10:43
  • @RichardDalton : what do you mean it applies to if? E.g.: if PropertyName == 1 && SubCollection.length ? should be if 'PropertyName' ? and for SubCollection.length ? Asking because I just have some problems with knockout 2.3.0 with IE8. Expected identifier, string or number line 2000 I put all my properties in quotes in the if as well. Same crap (I really hate IE) :(
    – firepol
    Aug 30, 2013 at 15:25
  • 1
    @firepol I mean that when you are doing a data-bind="if: something". The if should be quoted like so data-bind="'if': something". Aug 30, 2013 at 16:46
  • @RichardDalton thanks, you are the only person who points the 'if': something issue out. Do you know when this is the case, it only seems to be an issue for me currently in this line ko template: { name: 'conversation-template', data: negotiation, 'if': negotiation().conversation
    – nodrog
    Sep 16, 2013 at 18:15
  • Thanks a lot! My issue was with 'for'. So I changed data-bind="for: something" to data-bind="'for': something" and it fixed the IE8 binding issue.
    – bradjive
    Nov 7, 2013 at 17:14
4

Daniel A. White has already pointed out your specific problem, but if you were to be working with CSS classes on a regular basis, I would recommend using the css binding.

http://knockoutjs.com/documentation/css-binding.html

<div data-bind="css: { profitWarning: currentProfit() < 0 }">
   Profit Information
</div>

<script type="text/javascript">
    var viewModel = {
        currentProfit: ko.observable(150000) // Positive value, so initially we don't apply the "profitWarning" class
    };
    viewModel.currentProfit(-50); // Causes the "profitWarning" class to be applied
</script>

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.