views:

528

answers:

3

Hi guys,

I've got a checkbox inside a table, and when you click on it, it'll change the background colour accordingly like so...

$("#table tr td :checkbox").bind("click change", function() {
    $this = $(this); // cache the jquery object
    if($this.is(':checked')) { $this.closest('tr').css('background-color', 'lightyellow'); }
    else { $this.closest('tr').css('background-color', '#fff'); }
});

That works just fine, however, I figured I'd like to go one better, so anywhere on the table row you click, it'll check the box and highlight the row.

I tried using this code, but it doesn't work unfortunately:

$("table tr").bind("click", function() {
    $(this).parents("tr").find(":checkbox").attr('checked');
});

And here's the HTML code (removed excessive stuff to improve readability...

<td>Name</td>
<td>Description</td>
<td><input type="checkbox"></td>

Any help would be very much appreciated, thank you!

+1  A: 

You want to change this:

$(this).parents("tr").find(":checkbox").attr('checked');

to this:

$(this).parents("tr").find(":checkbox").attr('checked', 'checked');

Otherwise all you're doing is reading the checked attribute, not setting it.

VoteyDisciple
Very well spotted btw, and thanks for the explanation - cheers man!
Nick
+2  A: 

The event your handling is the tr click. The parent is the table so that will not help. All you need to do is use find() from the this context.

I would use .live to avoid multiple event handlers when one will do.

Assuming you only have one checkbox in the row then use the following. (note it uses tr's inside tbody to avoid running this on thead rows)

$("table>tbody>tr").live("click", function() {
    $(this).find(":checkbox").attr('checked', 'checked');
});

UPDATE

If you want to toggle it try something like

$("table>tbody>tr").live("click", function(ev) {
        var $checkbox = $(this).find(":checkbox");
        //check to see we have not just clicked the actual checkbox
        if ( !$(ev.target).is(':checkbox') ){
            $checkbox.is(':checked') ? $checkbox.removeAttr('checked')
                                     : $checkbox.attr('checked', 'checked')
        }
 });
redsquare
+1 for noticing that the `.parents('tr')` is not needed.
MitMaro
This is great, many thanks - just had a thought though, anyway to find out whether it's already checked?
Nick
yeah if ( $(this).find(":checkbox").is(':checked') )
redsquare
Like this? (sorry) $("table>tbody>tr").live("click", function() { if($(this).find(":checkbox").is(':checked')) { $(this).attr('checked', 'checked'); } else { $(this).removeAttr('checked'); } });
Nick
see above !
redsquare
dude, i'm so sorry - i'm really hoping this will be the last time i ask anything, but anyway to change the colour of the row too? so for example...if ($checkbox.is(':checked')) {$checkbox.removeAttr('checked'); $checkbox.parent('tr').css('background-color', 'lightyellow'); }else { $checkbox.attr('checked', 'checked'); $checkbox.parent('tr').css('background-color', '#fff'); }
Nick
$checkbox.parent().parent().css('background-color', 'lightyellow');That works, but it's probably dodgy as hell coding wise, heh!
Nick
Thanks once again btw red, you're a bloody star! Cheers
Nick
+1  A: 

I think you are just forgetting to set the value of the attribute.

$("table tr").bind("click", function() {
    $(this).find(":checkbox").attr('checked', 'checked');
});

The jQuery Docs on Attributes might be of some assistance.


Credit to redsquare for noticing that the .parent("tr") is not needed.

MitMaro
this is the tr, parents('tr') will not match anything unless he is in a nested table which I do not think he is.
redsquare
Thats why I upvoted yours. ;)
MitMaro
Many thanks mit, and red!
Nick