0

Very simple data-bind="checked: someBool" that isn't checking my radio input.

<input type="radio" data-bind="checked: someBool" />radio

The ko code...

var data = { someBool: true };
var vm = ko.mapping.fromJS(data);
ko.applyBindings(vm);

The radio is un-checked. Am I missing something?

Live: http://jsbin.com/exokav/4/edit

Ian Davis
  • 19,091
  • 30
  • 85
  • 133
  • possible duplicate of [Radio buttons Knockoutjs](http://stackoverflow.com/questions/16259537/radio-buttons-knockoutjs) – nemesv Jun 14 '13 at 13:45

1 Answers1

0

You need to add a value attribute for the radio-button to map to. Also the value won't be a boolean, but a string after this. I use the following extension to use boolean values for radio buttons. (live: http://jsbin.com/exokav/8):

ko.bindingHandlers.checkedAsBool = {
    init: function (element, valueAccessor, allBindingsAccessor) {
        var observable = valueAccessor(),
            interceptor = ko.computed({
                read: function () {
                    var val = ko.utils.unwrapObservable(observable);
                    return ((val !== null) && (typeof val !== "undefined") ? val.toString() : val);
                },
                write: function (newValue) {
                    observable(newValue === "true");
                },
                owner: this
            });
        ko.applyBindingsToNode(element, { checked: interceptor });
    }
};

Based on this answer by RP Niemeyer.

Community
  • 1
  • 1
mhu
  • 17,720
  • 10
  • 62
  • 93
  • Off topic: ;-) If you like this answer, maybe you can also mark a correct answer here: http://stackoverflow.com/q/9475893/932282 – mhu Jun 14 '13 at 13:52
  • lol you're right @mhu. done and done. back to this post, I'll mark this answer as the right one, altho it's bummer because knockout's website makes it look so easy :/ http://i.imgur.com/U0qah8P.png screenshot, link to full article here: http://knockoutjs.com/documentation/checked-binding.html – Ian Davis Jun 14 '13 at 14:10
  • Thanks, now I'm a guru ;-). Yes, but that sample also uses checkboxes for boolean values. – mhu Jun 14 '13 at 14:21