I am doing an experiment with Knockout, here's the JSFiddle
Radio button has a data-bind to the enableMe property of the View Model. If I click on the radio button, it updates the enableMe property and 'checked' data-bind gets triggered.
What I am trying to do is that, when I change the checked
attribute of the radio button (on button click event) and see whether the View Model changes. It does not change.
Why is that? Need to know how knock out works in this scenario.
var ViewModel = function() {
this.enableMe = ko.observable(false);
};
var myVM = new ViewModel();
ko.applyBindings(myVM);
function clickme() {
//Changing the checked state does not change VM
document.getElementById("myradio1").checked = true;
//Changing the VM works
//myVM.enableMe(true);
alert("Alert Message OnClick");
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.2.0/knockout-min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
EnableMe: <input id="myradio1" type="radio" data-bind='checked: enableMe'/><br/>
Enable status: <span data-bind='text: enableMe'></span>
<br/>
<button id="button1" onclick="clickme();">Click To Disable</button>