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

  • Home
  • SEARCH
  • 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 7403659
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T05:08:31+00:00 2026-05-29T05:08:31+00:00

I am using knockout js to set the class of an element depending on

  • 0

I am using knockout js to set the class of an element depending on the result of an expression, what I would like to know is if it is possible to set the class to the value returned from a function in the view model.

This is what i have at the moment and works:

<div data-bind="css: { highlightup : OneDayChange > 0 &&  SevenDayChange > 0}">
</div

I would like to have somthing like:

<div data-bind="css: { bothValuesIncreasing(); }">
</div

Edit
for clarification i would like the class to be set by the value returned from the function, but it may not be a boolean value, it could be any string the function returns

  • 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-05-29T05:08:31+00:00Added an answer on May 29, 2026 at 5:08 am

    You need to use computed properties.

    var vm = function(){
      var self = this;
      self.OneDayChange = ko.observable();
      self.SevenDayChange = ko.observable();
      self.isBothValue = ko.computed(function(){
        return self.OneDayChange() > 0 && self.SevenDayChange() > 0;
      });
    }
    
    <div data-bind="css: { highlightup: isBothValue() }">
    </div
    

    Don’t afraid to use those properties for view specific actions. Thats what viewmodels created are.

    Responding to update:
    Custom binding would be a best choise for solving this problem.

    Actually I found an answer on KnockoutJs google groups

    Bindings – https://github.com/SteveSanderson/knockout/wiki/Bindings—class

    Demo – http://jsfiddle.net/mbest/NBmjh/

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

Sidebar

Related Questions

Using the HTML5 <canvas> element, I would like to load an image file (PNG,
is it possible to use CMYK overprinting without using the CMYKColorSep class, which always
Does anyone know of an example application using knockout js and MVC 2? I'm
Using knockout.js, is it possible to bind to a property of a child object
I am binding a list of objects to a select using knockout. Object Class
I am currently developing using ASP.NET, and I'd like to get started on Knockout
Using JDeveloper , I started developing a set of web pages for a project
Using C#, I need a class called User that has a username, password, active
Using preview 4 of ASP.NET MVC Code like: <%= Html.CheckBox( myCheckBox, Click Here, True,
Using PHP I'd like to compare an actual ip address to part of one,

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.