I've got 2 checkboxes, if you check one box the other box will be deselected. But for some reason I can't deselect the box that has been checked
jQuery(document).ready(function(){
jQuery("input[type='checkbox']").on('click', function () {
jQuery("input[type='checkbox']").removeClass('selected').attr('checked', false);
jQuery("input[type='checkbox']").parent().removeClass('selected');
jQuery(this).parent().addClass('selected')
jQuery(this).attr('checked', true);
});
Any idea why this is happening?
solution (thanks to Shree)
$('input.example').on('change', function() {
if($(this).is(':checked'))
{
$('input.example').not(this).prop('checked', false);
$('input.example').parent().removeClass('selected');
$(this).parent().addClass('selected');
}
else
{
$('input.example').parent().removeClass('selected');
}
});
.selected {
background-color:red
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
<input type="checkbox" class="example" />
</div>
<div>
<input type="checkbox" class="example" />
</div>