jQuery: Uncheck other checkbox on one checked

Code Lover picture Code Lover · Jul 22, 2013 · Viewed 124.8k times · Source

I am having total 6 checkbox ( may be add more in future ) and I want to allow only select one so when user checked any of them other should unchecked.

I tried with this code and works fine if I defined ID but now just wonder how to make it vice versa in efficient way so in future if I add more than it wont be a problem

$('#type1').click(function() {
     $('#type2').not('#type1').removeAttr('checked');
}); 

FYI, checkboxs are not sibling and are in different <td>

Answer

billyonecan picture billyonecan · Jul 22, 2013

Bind a change handler, then just uncheck all of the checkboxes, apart from the one checked:

$('input.example').on('change', function() {
    $('input.example').not(this).prop('checked', false);  
});

Here's a fiddle