Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8924013
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T07:17:21+00:00 2026-06-15T07:17:21+00:00

In a <ul> foreach binding, I want knockout to add a dynamic css class

  • 0

In a <ul> foreach binding, I want knockout to add a dynamic css class to each <li>.

The objects I’m binding to look like this:

{
    id: 1234,
    class: "foo",
}

I want knockout to add the class property of each item to the corresponding <li>, like so:

<ul data-bind="foreach: TheList">
    <li data-bind="css: class" />
</ul>

So that my [effective] output is like this:

<ul>
    <li class="foo" />
    <li class="bar" />
    <li class="etc" />
</ul>

I can’t figure out the binding syntax. I’ve tried this,

<li data-bind="css: class" />

It throws an error:

Unable to parse bindings. Message: SyntaxError: Syntax error; Bindings value: css: class

I’ve tried this,

<li data-bind="css: $data.class" />

It throws an error:

Unable to parse bindings. Message: SyntaxError: Expected identifier; Bindings value: css: $data.class

I wrapped class in quotes to avoid js keyword conflict,

<li data-bind="css: 'class'" />

This does not throw an error, but it yields unexpected results. I’m 100% positive that “trim” is not one of the class property values of any item in the collection:

<li class="trim" data-bind="css: 'class'" __ko__1354588574237="ko3">

I don’t know how or if I can use a computed in this case because AFAIK I can’t pass a variable (like$index or $data) into the computed, therefore the computed can’t know which item to return class for.

What is the appropriate binding syntax for this scenario, is it possible?

Solution / Workarounds

This appears to be a css: binding issue in IE8 when the property is named ‘class’. Here are 3 different workarounds if you run into it:

Change the name of the field to something other than ‘class’ (if you have control of the source).

Or, you can modify the objects in the list so that the value is accessible from a different name, i.e.,

for (var i = 0; i < TheList.length; i++)
{
    TheList[i].safeName = TheList[i]['class'];
}

Or, you can use the attr: binding instead of the css: binding, which does not appear to suffer from the same issue and can use members called ‘class’ so long as they are encased in quotes:

<li data-bind="attr: { 'class': 'class' } />
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T07:17:22+00:00Added an answer on June 15, 2026 at 7:17 am

    this should work:

    <ul data-bind="foreach: TheList">
        <li data-bind="css: $data['class'], text: $data['class']" />
    </ul>​
    

    i think you’re running into a keyword issue with class. working jsfiddle.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to implement this simple animation in knockout: function scroll(back) { var scrollContainer
I want to add selected options from select element to binding table. The view
I have a <ul> element which is populated with a foreach binding. This <ul>
I need something like this: <WrapPanel Orientation=Horizontal> <TextBlock Text={Binding Path=Person.Name} Style=style1/> <TextBlock Text={Binding Path=Project.Name}
I want to create a 3x3 grid with images loaded using knockout binding <fieldset
In knockout.js 2.1.0, in a template using the foreach binding, you can access the
foreach (var person in peopleList.Where(person => person.FirstName == Messi)) { selectPeople.Add(person); } I am
I have a List box that I want to display a list of objects,
DataGridTextColumn col = new DataGridTextColumn(); col.Header = Text1; col.Binding = new System.Windows.Data.Binding(Text1); grd.Columns.Add(col); I
Am I right in saying that Knockout encodes HTML values when binding to a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.