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 from the array, once the last item is removed, the checkbox automatically unchecks itself.

The problem I'm having is that if you click to remove each array item, and then click the checkbox to add a blank entry, I want the checkbox to be checked again (based on which observable is checked), but it's not ?

I have the following code:

http://jsfiddle.net/UBsW5/3/

    <div>
        <input type="checkbox" data-bind="checked: PreviousSurnames().length > 0, click: $root.PreviousSurnames_Click" />Previous Surname(s)?
    </div>
    <div data-bind="foreach: PreviousSurnames">
        <div>
            <input type="text" data-bind="value: $data">
            <span data-bind="click: $root.removePreviousSurname">Remove</span>
        </div> 
    </div>


var myViewModelExample = function () {
    var self = this;

    self.PreviousSurnames = ko.observableArray(['SURNAME1', 'SURNAME2', 'SURNAME3']);

    self.removePreviousSurname = function (surname) {
        self.PreviousSurnames.remove(surname);
    };

    self.PreviousSurnames_Click = function () {
        if (self.PreviousSurnames().length === 0) {
            self.PreviousSurnames.push('');
        }
        else {
            self.PreviousSurnames.removeAll();
        }
        alet(2)
    }

}

ko.applyBindings(new myViewModelExample());
east david

You need to use a calculation to monitor the length of the observable array. This way you can automatically react to the length when it reaches zero.

    self.surnames = ko.computed(function() {
        var checked = true;
        if (self.PreviousSurnames().length === 0) {
            self.PreviousSurnames.push('');
            checked = false;
        }            
        return checked;
    });

Now, after clearing all names, you will see a blank text box. If you update the binding on the checkbox, the binding will work fine as well.

<input type="checkbox" data-bind="checked: surnames, click: PreviousSurnames_Click" />Previous Surname(s)?

violin

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 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

Enable click event when another checkbox is checked

bobc82 I need a jQuery function that does the following: For example, when checkbox1selected, and when I click on some other element (with an ID starting with it element-), I can print idthose elements in the console like this: $('#checkbox1').click(function()

Enable click event when another checkbox is checked

bobc82 I need a jQuery function that does the following: For example, when checkbox1selected, and when I click on some other element (with an ID starting with it element-), I can print idthose elements in the console like this: $('#checkbox1').click(function()

Enable click event when another checkbox is checked

bobc82 I need a jQuery function that does the following: For example, when checkbox1selected, and when I click on some other element (with an ID starting with it element-), I can print idthose elements in the console like this: $('#checkbox1').click(function()

Enable click event when another checkbox is checked

bobc82 I need a jQuery function that does the following: For example, when checkbox1selected, and when I click on some other element (with an ID starting with it element-), I can print idthose elements in the console like this: $('#checkbox1').click(function()

Enable click event when another checkbox is checked

bobc82 I need a jQuery function that does the following: For example, when checkbox1selected, and when I click on some other element (with an ID starting with it element-), I can print idthose elements in the console like this: $('#checkbox1').click(function()

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

Click event fires automatically on knockout js

lever I'm trying out the mail client tutorial at Learn.knockoutjs.com . I want to add a "back button" to the message details block. But the button is triggered automatically. My password <div class="viewMail" data-bind="with: chosenMailData"> <button data-

jQuery click checkbox not checked

Predrag Beocanin ok so i'm using the SUPERSIMPLE code to check my checkboxes. I have an invisible ahref for elements and a checkbox next to them. Here is the html: <a href="#" class="transparent-checkbox-thingy" id="<?php echo $id;?>" onclick="check(<?php echo

Prevent checkbox checked event

GM6 I'm checkboxasking the user if they are sure, otherwise I'll cancel the action. I tried the Clickevent, but it turns out that the CheckedChangedevent is only called after the event . I thought I could at least do it "ugly" by asking the user inside the Che

Prevent checkbox checked event

GM6 I'm checkboxasking the user if they are sure, otherwise I'll cancel the action. I tried the Clickevent, but it turns out that the CheckedChangedevent is only called after the event . I thought I could at least do it "ugly" by asking the user inside the Che