Knockout JS checkbox checked binding


gth685f

In knockout js, I am trying to perform a foreach on an array of data to display checkboxes. The problem I'm having is that the selected databinding doesn't seem to work until I interact with one of the boxes. For example, below I will generate 5 textboxes, none of which will appear selected. However, when I click on "one", "two" and "four" are also checked as they should be there from the beginning.

Javascript:

var viewModel = {};

viewModel.choices = ["one", "two", "three", "four", "five"];
viewModel.selectedChoices = ko.observableArray(["two", "four"]);

viewModel.selectedChoicesDelimited = ko.dependentObservable(function () {
        return viewModel.selectedChoices().join(",");
    });

ko.applyBindings(viewModel);

HTML:

<ul class="options" data-bind="foreach: choices">
    <li><label><input type="checkbox" name="NotifyMembers" data-bind="checked: $parent.selectedChoices, attr: { value: $data }" /><span data-bind="text: $data"></span></label></li>
</ul>
<hr />
<div data-bind="text: selectedChoicesDelimited"></div>

The fiddle is at : http://jsfiddle.net/bvGG3/1/

thank you for your help.

nemesv

In knockouts prior to version 3.0, bindings fire sequentially, so your problem is that checkedbindings fire before attrbindings .

So you need to change the order of bindings:

<input type="checkbox" name="NotifyMembers" 
       data-bind="attr: { value: $data }, checked: $parent.selectedChoices" />

Demo JSFiddle .

Otherwise, your original code will be available when you update to 3.0 (demo JSFiddle ) .

Related


Knockout JS checkbox checked binding

gth685f In knockout js, I am trying to perform a foreach on an array of data to display checkboxes. The problem I'm having is that the selected databinding doesn't seem to work until I interact with one of the boxes. For example, below I will generate 5 textbo

Knockout.js checkbox checked and click event

David Reid We are trying to implement checkboxes and lists with the following functionality: Clicking the checkbox will clear the array (if there are items in it), or add a new item (if there are none). When the delete button is clicked, an item is removed fro

How to change checkbox checked value in Knockout.js?

stereotaxic I want to change the value of the checkbox via jQuery, but the knockout binding doesn't work var viewModel = { myValue: ko.observable(true) }; ko.applyBindings(viewModel); $(':checkbox').prop({checked: false}).change(); http://jsfiddle.net/s

Knockout.js data binding array to checkbox list

Michael Marohnic Patient 1 Patient.Age = '25' Patient.Injury[0].Date = '2015-01-01' Patient.Injury[0].Type = 'Burned' Patient.Injury[1].Date = '2015-01-27' Patient.Injury[1].Type = 'Sprained Ankle' Patient 2 Patient.Age = '17' Pati

Knockout.js data binding array to checkbox list

Michael Marohnic Patient 1 Patient.Age = '25' Patient.Injury[0].Date = '2015-01-01' Patient.Injury[0].Type = 'Burned' Patient.Injury[1].Date = '2015-01-27' Patient.Injury[1].Type = 'Sprained Ankle' Patient 2 Patient.Age = '17' Pati

Checkbox knockout click binding not working

Manisankar I know Knockout expects us to return a function truebound to an clickevent in order to check/uncheck a checkbox. I tried the following code but it doesn't check the checkbox. I can display the values using anonymous computed functions, but my array

Checkbox checked property binding - polymer

Dante I'm trying to bind a Polymerproperty to CheckBoxthe checked property. However, the observer for the property is never fired, and furthermore, the label never displays any text. However, each CheckBoxclick will be able to perform a function . Here is my c

Checkbox checked property binding - polymer

Dante I'm trying to bind a Polymerproperty to CheckBoxthe checked property. However, the observer for the property is never fired, and furthermore, the label never displays any text. However, each CheckBoxclick will be able to perform a function . Here is my c

Checkbox checked property binding - polymer

Dante I'm trying to bind a Polymerproperty to CheckBoxthe checked property. However, the observer for the property is never fired, and furthermore, the label never displays any text. However, each CheckBoxclick will be able to perform a function . Here is my c

Checkbox checked property binding - polymer

Dante I'm trying to bind a Polymerproperty to CheckBoxthe checked property. However, the observer for the property is never fired, and furthermore, the label never displays any text. However, each CheckBoxclick will be able to perform a function . Here is my c

Checkbox checked property binding - polymer

Dante I'm trying to bind a Polymerproperty to CheckBoxthe checked property. However, the observer for the property is never fired, and furthermore, the label never displays any text. However, each CheckBoxclick will be able to perform a function . Here is my c

Checkbox checked property binding - polymer

Dante I'm trying to bind a Polymerproperty to CheckBoxthe checked property. However, the observer for the property is never fired, and furthermore, the label never displays any text. However, each CheckBoxclick will be able to perform a function . Here is my c

Uncheck checkbox in knockout js

I'm html <input id="checkbox" name="claimcheckbox" type="checkbox" data-bind="checked: IsclaimcheckboxSubscribe,visible: Authorization.IsclaimcheckboxAuthorized" /> Java script IsclaimcheckboxSubscribe = ko.observable(); IsclaimcheckboxSubscribe.subscribe(fun

Uncheck checkbox in knockout js

I'm html <input id="checkbox" name="claimcheckbox" type="checkbox" data-bind="checked: IsclaimcheckboxSubscribe,visible: Authorization.IsclaimcheckboxAuthorized" /> Java screenplay IsclaimcheckboxSubscribe = ko.observable(); IsclaimcheckboxSubscribe.subscribe

Knockout js CSS binding

kitten salvaji I am using Knockout js in my project and I need to add multiple css class names to specific tags. Below is my code, I have three different classes, how can I add it to the knockout css binding, kindly suggest. <img data-bind="attr: { src:Profile

knockout js check binding

username I'm new to knockout and also a little confused about checkbox bindings. I have the following HTML: <input id="Category-2039842085241261997" type="checkbox" value="203984,208524,1261997" data-bind="checked:toggleActivation"> <label for="Category-2039

Knockout js binding not updating

User 135498 I have a view model that contains a reference to another object, and the referenced object is bound to my UI. When I add objects to the observable array, the bindings seem to work fine, but when I clear the array, the bindings don't seem to update.

Knockout js binding not updating

User 135498 I have a view model that contains a reference to another object, and that referenced object is bound to my UI. When I add objects to the observable array, the bindings seem to work fine, but when I clear the array, the bindings don't seem to update

knockout js check binding

username I'm new to knockout and also a little confused about checkbox bindings. I have the following HTML: <input id="Category-2039842085241261997" type="checkbox" value="203984,208524,1261997" data-bind="checked:toggleActivation"> <label for="Category-2039

Knockout js binding not updating

User 135498 I have a view model that contains a reference to another object, and that referenced object is bound to my UI. When I add objects to the observable array, the bindings seem to work fine, but when I clear the array, the bindings don't seem to update

Binding knockout updates in .js

Mike W This is most likely a very basic question for anyone familiar with ockout.js, but it's causing me problems. The situation I'm having is that I have a model that contains an array of items that are dynamically added to and displayed in a view. So far, no

Checkbox doesn't stay checked with databinding in knockout

Tom Nolan I have a checkbox and I want to execute a function when the function is clicked. The problem is, it needs to perform other functions depending on whether the checkbox is checked or not. So I set up an observable state, which is set to false by defaul

Knockout: Add text path only if checkbox is checked

database I am playing with SVG. I have a checkbox. If checked, I want to use the text path with the text path. If the checkbox is not checked then I only want the text to be displayed without the text path. The problem is that the data-bind attribute should be

Checkbox doesn't stay checked with databinding in knockout

Tom Nolan I have a checkbox and I want to execute a function when the function is clicked. The problem is, it needs to perform other functions depending on whether the checkbox is checked or not. So I set up an observable state, which is set to false by defaul