1

I want to add a dynamic classname which will come from javascript based on some check. But one class navigate-forward is there which is also displayed based on some check. How to combine both?

Following is my code -

<span class="menu-item" data-bind="text: data.description,
            css: {
                    'navigate-forward': !child.action,
                    child.className //I tried something like this which doesn't work
            }"></span>

child.className will have a value based on some check in javascript code. And whatever value it gives I want to add that class to this span.

Ex. if child.className is tooltip then that class should be added to the span.

output

<span class="menu-item navigate-forward tooltip">Menu item</span>
1

3 Answers 3

0

The css: binding only accepts a list of classes and whether to apply them or not. You could use:

<div data-bind="attr: { 'class': MyPropertyName }" class="initialClass"></div>

But this would overwrite any pre-existing classes (initialClass) on the element.

Take a look at https://github.com/knockout/knockout/wiki/Bindings---class where there is a custom binding handler that allows you to specify a dynamic class:

<div class="initialClass" data-bind="class: MyPropertyName"></div>

This solution will combine the existing class attribute with the MyPropertyName property.

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

Comments

0

function vm(){
  var self = this;
  self.className = ko.observable("class2");
}


(function(){
var viewModel = new vm();
ko.applyBindings(viewModel);
})();
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.2/knockout-min.js"></script>
<label class="class1" data-bind="css:className">Your Element</label>

Comments

0

You can also use the style and css bindings with an object that holds all your styles and classes instead of using individual class names. This allows for a more programmatic approach.

function viewModel(){
  var self = this;
  this.description = ko.observable('placeholder text');
  this.useBlue = ko.observable(false);
  this.borderClass = ko.observable("red-border");

  this.classes = ko.pureComputed(function(){
    var obj = {
      'blue-background': self.useBlue()
    };
    obj[self.borderClass()] = true;
    return obj;
  });
}

ko.applyBindings(new viewModel());
.blue-background {
  background-color: lightblue;
}

.red-border {
  border: 1px solid red;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.2/knockout-min.js"></script>

<span class="menu-item" data-bind="text: description, css: classes"></span>
<br/>
            
<input type="button" data-bind="click: function(){ useBlue(!useBlue()) }" value="toggle" />

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.