How to add addition class in JS to the div or table , when radio input is checked ?
I would like add class table-selected , to the div class="table-item" when I checked a radio input .
$(".table-item input").change(function() {
var div = $(this).closest('.table-item');
$(this).is(":checked") ? div.addClass("table-selected") : div.removeClass("table-selected");
});
#test .table-item {
border: solid 1px #666;
height: 25px;
background-color: #CCC
}
#test .table-selected {
border: 1 px solid #F00;
background-color: #00F
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="test">
<div class="table-item">
<div><label for="check1"><input type='radio' name='rad' value='1'>1</label></div>
</div>
<div class="table-item">
<div><label for="check2"><input type='radio' name='rad' value='2'>2</label></div>
</div>
<div class="table-item">
<div><label for="check3"><input type='radio' name='rad' value='3'>3</label></div>
</div>
</div>
But this work some wrong , add class , but it does not remove it when unchecked