Change style based on checkboxes w/jQuery

Posted by santa on Stack Overflow See other posts from Stack Overflow or by santa
Published on 2011-11-30T01:11:23Z Indexed on 2011/11/30 1:51 UTC
Read the original article Hit count: 492

Filed under:
|
|

I need to change the style of the entire row when a checkbox in that row is checked or unchecked.

function myFunc() {
    if ($(".chkbx:checked").length) {
        $(this).parent().parent().css("background-color","red");
    } else {
        $(this).parent().parent().css("background-color","green");
    }
}

myFunc();

$(".chkbx").change(myFunc);
<table>
    <tr>
        <td><input type="checkbox" class="chkbx" checked></td>
        <td>label 1</td>
    </tr>
    <tr>
        <td><input type="checkbox" class="chkbx" checked></td>
        <td>label 1</td>
    </tr>
    <tr>
        <td><input type="checkbox" class="chkbx" checked></td>
        <td>label 1</td>
    </tr>
</table>

I think I'm missing something here...

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about jQuery