I am writing a plugin using jQuery and knockout. I have two radio buttons. I am using knockout data-bind to check and uncheck the radio button. The problem is that when I am trying to uncheck the radio button on click of another button using jQuery, it is not updating bind observable property .
<input type="radio" data-bind="checked: selectedVal" name="1" value="fixedPrice"/> Fixed Price
<input class="hn" type="radio" data-bind="checked: selectedVal" name="1" value="allowBiding"/> Allow Biding
<pre data-bind="text:ko.toJSON($data,null,2)"></pre>
<input type="button" id="button" value="Click Me" />
var onClick = function() {
$('.hn').prop('checked', true);
};
$('#button').click(onClick);
var ViewModel = function () {
var self = this;
self.selectedVal = ko.observable("fixedPrice");
self.selectedVal.subscribe(function (val) {
console.log(val)
});
};
ko.applyBindings(new ViewModel());
Please find this jsfiddle below with more details.
Welp! Don't mix KO and jQuery in this way. You're fighting Knockout, not using it. See this earlier answer I wrote for a very similar situation and extended explanation.
Note that this is certainly not a bug, jQuery will by default not trigger events on DOM changes like that. If you insist on mixing KO and jQuery this way, be sure to notify others like this:
jQuery and Knockout are fighting over control of the view. If you're going to use a viewmodel, use the viewmodel and do not manipulate the DOM except through the viewmodel. You have a viewmodel element that controls the radio buttons, you just need to set it. Knockout provides a
click
binding, so you don't need jQuery to attach that, either.It looks like a bug. However, you can try to invoke the native click handler so the observable will be updated.
Or
Here is a JsFiddle Demo