stereodenis
stereodenis

Reputation: 3745

How to change checkbox checked value in Knockout.js?

I want to change checkbox value by jQuery, but Knockout bindings not working

var viewModel = {
    myValue: ko.observable(true)
};

ko.applyBindings(viewModel);

$(':checkbox').prop({checked: false}).change();

http://jsfiddle.net/stereodenis/n7zxvu01/3/

Upvotes: 1

Views: 3048

Answers (1)

TSV
TSV

Reputation: 7641

It is described in the Knockout.js documentation: checked binding.

<p>Send me spam: <input type="checkbox" data-bind="checked: wantsSpam" /></p>

<script type="text/javascript">
var viewModel = {
    wantsSpam: ko.observable(true) // Initially checked
};

// ... then later ...
viewModel.wantsSpam(false); // The checkbox becomes unchecked
</script>

Upvotes: 1

Related Questions