Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

knockoutjs checkbox and click on parent td

I have a table with a row. The row has a TD(with a checkbox in it) and on the TD I have a click function. So that when the TD is clicked the checkbox will get checked/unchecked.

It works fine when I click on the TD , but when clicking on the checkbox the (visual) value of the checkbox does not change( It does not get checked/unchecked )

The wanted situation is:

  1. When I click the checkbox, the (visual) value of the checkbox changes and I can call a function.( for example to make an AJAX call )

  2. When I click on the TD, the (visual) value of the checkbox changes and I can call a function. ( for example to make an AJAX call )

How can we achieve this?

Sample Code

like image 558
Blottt Avatar asked May 30 '12 20:05

Blottt


2 Answers

To avoid the click event issues, use the label element to make a larger area clickable. Here I've made the label a block element so it takes up the whole td:

<td>
    <label style="display: block">
        <input type="checkbox" data-bind="checked: checkBox" />
    </label>
</td>

See http://jsfiddle.net/mbest/LsxSh/

like image 123
Michael Best Avatar answered Oct 23 '22 12:10

Michael Best


Td Event seems to be overriding the the input's check click event

like image 43
Shay Avatar answered Oct 23 '22 13:10

Shay