Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CheckBox not getting checked in Knockoutjs

I am using a checkbox with a function inside the data-bind, but I am unable to check the checkbox.

view:

<input type="checkbox" data-bind="click: function(){ f('hello parameter'); }">Click me

View Model:

var VM = function () {
    this.f = function (param) {
        alert(param); // here i am getting 'hello parameter'
        return true;
    }
}

ko.applyBindings(new VM());

Here is my Fiddle

like image 227
super cool Avatar asked Oct 14 '14 07:10

super cool


People also ask

How do you activate a Knockoutjs model?

Activating Knockout To activate Knockout, add the following line to a <script> block: ko. applyBindings(myViewModel); You can either put the script block at the bottom of your HTML document, or you can put it at the top and wrap the contents in a DOM-ready handler such as jQuery's $ function.

Is checked in knockout JS?

When the user checks or unchecks the checkbox, Knockout will set your model property to true or false accordingly. If the checkedValue parameter is set, that value is used instead of true to represent a checked status, and an unchecked status is represented with a value of undefined .


1 Answers

By default, the click binding prevents the default reaction to a click based on the assumption that your JavaScript click event handler will handle everything. You need to return "true" to get the default behavior anyway, which you are doing from your f() function but not the wrapper inside data-bind:

<input type="checkbox" data-bind="click: function() { f('hello parameter'); }">

should be

<input type="checkbox" data-bind="click: function() { return f('hello parameter'); }">
like image 103
Niko Avatar answered Sep 25 '22 02:09

Niko