Nick Developer
Nick Developer

Reputation: 287

unable to make checkbox checked in knockoutjs

I have this code sample:

http://jsfiddle.net/nickbuus/eUufc/15/

where I am trying to make the checkbox have a normal check/uncheck behaviour. It works fine untill I add an onclick event to each checkbox. This onclick event calls this method:

 self.saveIsMeasureChecked = function (item) 

After this method ends the checkbox gets back to its old state before being checked/unchecked - Which makes it appear that you cannot change the state of the checbox at all.

Upvotes: 3

Views: 1385

Answers (1)

nemesv
nemesv

Reputation: 139758

You need to return true from your click event handler in order to trigger the browser default behavior (see also in the documentation: Allowing the default click action section)

self.saveIsMeasureChecked = function (item) {
    //Do something
    return true;
};

Demo JSFiddle.

However it seems there is something not quite right in Knockout because when you are using the value binding and the click binding on the checkbox you need to click twice for the first time to make the checkbox change.

But if your are using the attr binding to set the value everything seems OK.

<input name="cbIsMeasureChecked" type="checkbox" 
       data-bind="attr: {value: foodId}, 
       checked: isMeasureChecked, 
       click: $root.saveIsMeasureChecked" />

Demo JSFiddle.

Upvotes: 4

Related Questions