I am trying to get my td to change color if the user clicks the td itself or the checkbox.
Right now if the user clicks the td the css will change and the checkbox will check/uncheck.
But if the checkbox is clicked, nothing changes. additionally if the page is refreshed the checkboxes are checked, but the CSS is gone.
What am I doing wrong?
<table>
<tr><td><input type="checkbox" name="my-group[]" value="1">A</td></tr>
<tr><td><input type="checkbox" name="my-group[]" value="2">B</td></tr>
<tr><td><input type="checkbox" name="my-group[]" value="3">C</td></tr>
<tr><td><input type="checkbox" name="my-group[]" value="4">D</td></tr>
</table>
<script>
var chosen = {'background-color': '#9F1313', 'color': '#fff'};
var unchosen = {'background-color': 'transparent', 'color': '#000'};
$('td').click(function (event) {
if (!$(event.target).is('input')) {
$('input:checkbox', this).prop('checked', function (i, value) {
highlight(this, $(this).closest('td'));
return !value;
});
}
});
function highlight(cb, cell) {
if (cb.checked) {
$(cell).css(unchosen);
} else {
$(cell).css(chosen);
}
}
</script>