I am new to Knockout. I am trying to create a List of Checkboxes, I followed the Answer here:
Working with a list of checkboxes in knockoutjs
But I am getting an error when trying to Bind the CHECKED Property of the Checkboxes.
The HTML:
<ul data-bind="template: { name: 'choiceTmpl', foreach: EnquiryTypeList, templateOptions: { selections: SelectedEnquiryTypes } }"></ul>
<script id="choiceTmpl" type="text/html">
<li>
<input type="checkbox" data-bind="attr: { value: $data.Id }, checked: SelectedEnquiryTypes" />
<span data-bind="text: $data.Text"></span>
</li>
</script>
The JavaScript:
var viewModel = {
EnquiryTypeList: [new EnquiryType(1, "Text 1"), new EnquiryType(2, "Text 2")],
SelectedEnquiryTypes: ko.observableArray()
};
function EnquiryType(id, text){
Id = id,
Text = text
};
ko.applyBindings(viewModel);
It works when I put 1 or 0 in place of checked: SelectedEnquiryTypes
<ul data-bind="template: { name: 'choiceTmpl', foreach: EnquiryTypeList, templateOptions: { selections: SelectedEnquiryTypes } }"></ul>
<script id="choiceTmpl" type="text/html">
<li>
<input type="checkbox" data-bind="attr: { value: $data.Id }, checked: 1" />
<span data-bind="text: $data.Text"></span>
</li>
</script>