1

I am trying to apply CSS style using data-bind based on the current radio button being selected.

Below is the code I tried to apply but is not working..

<input type="radio" value="mtn" data-bind="checked: setRadioVal, css: {lblstylebold: checkRadioEnabled(this)}" id="mtnradio">
    <input type="radio" value="atn" data-bind="checked: setRadioVal, css: {lblstylebold: checkRadioEnabled(this)}" id="atnradio">

var ViewModel = {
   setRadioVal: ko.observable(null),
   checkRadioEnabled: function(value) {
   if (value == ViewModel.setRadioVal())
   return true;
   else
   return false;
   }
}
ViewModel.setRadioVal("mtn") // This gets sets initially with either mtn or atn based on ajax response which I have not posted here. Just for understanding I have set as mtn.

So once user selects one of the radio button, setRadioVal gets updated with either mtn or atn. And I am trying to call the function checkRadioEnabled and return true when current radio button value selected is equal to the enabled value. But css class is not getting applied.

When I debugged, I see it is going inside the function checkRadioEnabled when radio button is clicked, but value parameter is coming as a window object. How to pass radio button value and access it inside the function checkRadioEnabled? Am I doing anything wrong here?

1 Answer 1

1

here is a fiddle http://jsfiddle.net/LkqTU/31964/

   <input type="radio"
      class="enabled"
      name="flavorGroup"
      value="mtm" 
      data-bind="checked: selectedValue" />
    <span data-bind="css: { enabled: selectedValue() === 'mtm' }">mtm</span>
    <input type="radio" 
     name="flavorGroup" 
     value="atm" 
    data-bind="checked: selectedValue" />
   <span data-bind="css: { enabled: selectedValue() === 'atm' }">atm</span>

js

function model() {
  var self = this;
  selectedValue = ko.observable("atm")
}

var mymodel = new model();

$(document).ready(function() {
  ko.applyBindings(mymodel);
});
Sign up to request clarification or add additional context in comments.

1 Comment

Sorry for the delay in accepting and thanks for the answer !

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.