How can I dynamically add a CSS class to a radio button based on its selection?
$("input[type=radio][name=align]").on('change', function() {
var radVal = $("input[type=radio][name=align]").val();
if (radVal == 'center') {
$(".tlt1").addClass('centerCssClass').change();
$(".tlt1").removeClass('topCssClass').change();
} else if (radVal == 'top') {
$(".tlt1").addClass('topCssClass').change();
$(".tlt1").removeClass('centerCssClass').change();
}
});
.centerCssClass {
background-color: blue;
}
.topCssClass {
background-color: red;
}
.tlt1 {
float: left;
height: 300px;
width: 300px;
background-color: orange;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="radio" name="align" value="center">Center<br>
<input type="radio" name="align" value="top">Top<br>
<div class="tlt1"></div>