13

I have a scenario where the Click event is bind to <tr> and checked value is bind to checkbox inside the <td? inside <tr> . Now the problem is When the row is clicked the event is fired thats fine . But in case of I click the checkbox the observable value changes for row as well and fires the row click event . How to avoid this scenario

<tbody data-bind="foreach:Mails">
    <tr data-bind="click:$root.navigateToMail">
        <td style="width: 15px">
            <input type="checkbox" data-bind="checked: isSelected">
            @*<input type="checkbox">*@
        </td>
        <td data-bind="text: From">
        </td>
        <td data-bind="text: To">
        </td>
        <td data-bind="text: Subject">
        </td>
        <td data-bind="text: MailDate">
        </td>
    </tr>
</tbody>

The events are :

 ko.utils.arrayForEach(data.mails, function (mail) {
                    mail.isSelected = ko.observable(false);
                    mail.isSelected.subscribe(function (myvalue) {
                        console.log(myvalue);
                    });
                });
 self.navigateToMail = function (mail) {
        location.hash = mail.Folder() + '/' + mail.Id();
    };

I trimmed of unnecessary codes . Just have put where the problem has occurred .

Joy
  • 6,438
  • 8
  • 44
  • 75

1 Answers1

25

You will essentially need to cancel the event bubbling on click.

Here's an example of how you can do that:

<div data-bind="click: parentClick">
    <input type="checkbox" data-bind="checked: isSelected, click: function(){return true}, clickBubble: false">
</div>​

See here: http://jsfiddle.net/2M9XP/1/

badsyntax
  • 9,394
  • 3
  • 49
  • 67
  • 1
    Thanks! I was trying it with the `clickBubble:false`, but I didn't realize I had to explicitly handle the click event. Sort of thought the checked binding would cover it. – jes Aug 21 '13 at 21:43
  • I've been triying to fix this for hours. Thanks a lot! – Axel Prieto Mar 26 '14 at 15:39