add dynamic class with css binding

add dynamic class with css binding

August 28, 2026 · Illustrated author avatarBy Stefan Bogdanescu
add dynamic class with css binding

Adding Dynamic CSS Class with Knockout.js Binding in Magento

Knockout.js is a popular JavaScript library that provides a way of binding data to HTML elements for rapid, declarative, and maintainable UIs. In this post, we’ll explore how to add dynamic classes to an element using the css binding.

Understanding the Problem

You’re trying to apply a CSS class based on the value returned by the getCode() function in your Magento 2 payment method view:

<div class="payment-method" data-bind="css: {'_active': (getCode() == isChecked())}">
    <!-- ... -->
</div>

The current setup seems to work, but you want to apply multiple classes dynamically. You’ve tried adding the getCode() function directly to the binding:

<div class="payment-method" data-bind="css: {'_active': (getCode() == isChecked()), getCode()}">
    <!-- ... -->
</div>

However, this results in a syntax error:

Uncaught SyntaxError: Unable to parse bindings.
Bindings value: css: {'_active': (getCode() == isChecked()), getCode()}
Message: Unexpected token }

Solving the Issue

To apply multiple dynamic classes using css binding, you need to create an observable array or a function that returns an object containing your desired class names. Here are two solutions:

1. Using an Observable Array

var viewModel = {
    getCode: ko.observable('standard'),
    isChecked: ko.observable(false),
    dynamicClasses: ko.computed(function() {
        return { '_active': this.getCode() == this.isChecked() };
    }, viewModel)
};

Then, in your HTML:

<div class="payment-method" data-bind="css: dynamicClasses">
    <!-- ... -->
</div>

2. Using a Function

If you prefer to use a function instead of a computed observable, you can do something like this:

var viewModel = {
    getCode: ko.observable('standard'),
    isChecked: ko.observable(false),
    getDynamicClasses: function() {
        return { '_active': this.getCode() == this.isChecked() };
    }
};

And in your HTML:

<div class="payment-method" data-bind="css: getDynamicClasses">
    <!-- ... -->
</div>

Applying Multiple Classes

If you want to apply multiple classes without overwriting the initial payment-method class, you can use the attr binding along with the class property:

<div class="payment-method" data-bind="css: {getCode()}, attr: {'class': getAllClasses()}">
    <!-- ... -->
</div>

In your view model:

var viewModel = {
    getCode: ko.observable('standard'),
    isChecked: ko.observable(false),
    getAllClasses: function() {
        var classes = ['payment-method', this.getCode()];
        if (this.getCode() == this.isChecked()) {
            classes.push('_active');
        }
        return classes.join(' ');
    }
};

Conclusion

In summary, to apply dynamic CSS classes using the css binding in Knockout.js within a Magento 2 context, you need to create an observable array or a function that returns an object containing your desired class names.

Remember that if you want to apply multiple classes without overwriting the initial class, use the attr binding along with the class property. This ensures that all necessary classes are applied and maintained as expected.

For more in-depth tutorials on working with Knockout.js in Magento 2, I recommend checking out our dedicated resources at Laravel Company.

Magento 2 API Themes Pricing

Our network