3

I want to make it so that if you click anywhere inside a table cell, the checkbox in the cell is toggled. It should also work normally if the checkbox is clicked directly.

Here I used a red bordered blank "label" as the clickable area. Ideally I'd like to avoid labels.

http://jsfiddle.net/WPZf2/

<tr>
    <td><label><input type="checkbox"/></label></td>
    <td><input type="checkbox"/></td>
    <td><input type="checkbox"/></td>
</tr>   

Here the table cell click is being detected.

http://jsfiddle.net/WPZf2/1/

    $('td').click(function(){
        alert('click!');

If false is returned and the checkbox is clicked, the checkbox initially checked and then when you dismiss the alert popup the checkbox clears again. (at least in Chrome, Firefox and IE11)

http://jsfiddle.net/WPZf2/2/

    $('td').click(function(){
        alert('click!');
        return false;

I tried this with return false and no return. If you click on the cell it works but it doesn't work if you click the checkbox.

http://jsfiddle.net/WPZf2/3/

var $elem = $(this).find('input');
$elem.prop('checked', !$elem.prop('checked'));

If I do $elem.click() with no return statement it has the same problem.

http://jsfiddle.net/WPZf2/4/

$elem.click();

If I have "return false" it doesn't work at all:

http://jsfiddle.net/WPZf2/5/

So basically I want to be able to click anywhere in a cell including directly on the checkbox to toggle the checkboxes.

5 Answers 5

10

Here is another alternative way.

$(function(){
   $('td').click(function (event) {
     if (!$(event.target).is('input')) {
        var obj =$(this).find('input');      
        obj.prop('checked', !obj.is(':checked'));      
       }
   });
});

FIDDLE

Sign up to request clarification or add additional context in comments.

1 Comment

It's good that it is just in one function rather than two though.
4

http://jsfiddle.net/WPZf2/18/

$('td').click(function(){
    $(this).find(':checkbox').click();
});

$(":checkbox").click(function(e) {
     e.stopPropagation();        
});

Comments

1

Try this

$(function(){
    $('td').click(function(){
        //alert('click!');
        var $elem = $(this).find('input');
        $elem.prop('checked', !$elem.prop('checked'));
        //$elem.click();
//        return false;
    });
    $('input[type="checkbox"]').click(function(){
        $(this).prop('checked',!$(this).prop('checked'));
    })
});

Comments

1

Try this:

$('td, input').click(function (event) { // add input in the selector
    event.stopPropagation(); // stop the event bubble up
    var tgl = !$(event.target).is('input') && 
              !$('input:checkbox:checked', this).length ? true : false;
     // above we checked if target is not input and checked input is 
     // not there initially
    $('input:checkbox', this).prop('checked', tgl);
});  //---------------------------------------^^^---passing the boolean value

Demo

3 Comments

A line can be shortened to this: var tgl = !$(event.target).is('input') && !$(':checked', this).length;
@LukeWenke this is just for readablity and removing the scroll in this answer post. although demo has the single line.
I changed "input:checkbox:checked' to ':checked' and also removed '? true : false'.
0

Found an answer!

jQuery click on td to toggle checkbox

http://jsfiddle.net/WPZf2/6/

$('td').click(function (event) {
  if (!$(event.target).is('input')) {

      $('input:checkbox', this).prop('checked', function (i, value) {
         return !value;
      });
   }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.