0

I want to preselect the checkbox based on the data but checkbox are not getting preselected not sure why.

Below is the code.

Knockout

 salesPerson: ko.observableArray([
                { itemName: 'Choice 1' ,checked:true },
                { itemName: 'Choice 2' ,checked:false}
            ]),
 chosenSalesPersons: ko.observableArray()

HTML Code

 <div class="chechbox-wrap" data-bind="foreach: salesPerson,  event: { scroll: function(){} }">
                        <div data-bind="text: itemName"></div>
                            <label>
                        <input type="checkbox" data-bind="checkedValue: $data, checked: $parent.chosenSalesPersons" />
                                <span></span></label>
                        </div>
1
  • if i change to this it works but my choosenSalesperson functionality gets affected <input type="checkbox" data-bind="checkedValue: $data, checked: checked" /> Commented Nov 11, 2019 at 13:23

1 Answer 1

0

Just try replacing your code with below code:

      salesPerson: ko.observableArray([
            { itemName: 'Choice 1'},
            { itemName: 'Choice 2'}
        ]),
          chosenSalesPersons: ko.observable('Choice 1')
2
  • i want to show choice 1 as checked .this doesnt work Commented Nov 11, 2019 at 13:19
  • please check i have updates my answer Commented Nov 11, 2019 at 13:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.