JSFiddle - React, Tailwind, and code Playground
HTML
<script src="http://ajax.aspnetcdn.com/ajax/knockout/knockout-3.0.0.js"></script>
<div data-bind="foreach: ids" class="">
<label>
<input type="radio" name="someid" data-bind="value: value, checked: $root.selectedid" />
<span data-bind="text: text"></span>
</label>
</div>
<br/>
<input type="button" data-bind="click: test1" value="Test1"/>
<input type="button" data-bind="click: test2" value="Test2"/>
<input type="button" data-bind="click: test3" value="Test3"/>
JavaScript
function radiodata(text,value){
this.text = text;
this.value= value;
}
var viewModel = function(){
var self = this;
self.selectedid = ko.observable();
self.ids = ko.observableArray([
new radiodata('one',1),
new radiodata('two',2),
new radiodata('three',3)
]);
self.test1 = function(){
self.selectedid(2);//this doesnot work
};
self.test2 = function(){
self.selectedid('2');//this works
};
self.test3 = function(){
self.selectedid((3).toString());//this was my hack
};
}
var model = new viewModel();
ko.applyBindings(model);