jQuery Mobile Radio with Knockout JS

For those of you that are trying to use the jQuery Mobile Radio with Knockout JS… I was not able to get the jQuery Mobile radio boxes to work properly with Knockout JS bindings, so I created a custom binding to solve the problem…

    ko.bindingHandlers.mobileradio = {
        init: function (element, valueAccessor) {
        },
        update: function (element, valueAccessor) {
            var value = valueAccessor();
            var valueUnwrapped = ko.utils.unwrapObservable(value);
            if (valueUnwrapped == $(element).val()) {
                $(element).prop("checked", "true").checkboxradio("refresh");
            } else {
                $(element).removeProp("checked").checkboxradio("refresh");
            }
        }
    };

<input type="radio" name="myOption" id="myOption1" value="1" data-bind="checked: MyOption, mobileradio: MyOption" />
Peter Femiani About Peter Femiani

Peter is currently a systems architect for a leading social networking company.

Comments

  1. Rami says:

    This works perfectly, thanks a lot.

Speak Your Mind

*