Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery: Uncheck other checkbox on one checked

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>

like image 474
Code Lover Avatar asked Jul 22 '13 10:07

Code Lover


People also ask

How do you uncheck a checkbox when another is checked in react?

To uncheck a checkbox programmatically in React, we can set the checked prop of the checkbox to a state. We have the checked state that we used to set the checked prop of the checkbox. Then we add a button that calls setChecked to toggle the checked value when we click the button.

How do you uncheck a selected checkbox when one checkbox is unchecked?

Select change eventCheck total options and total selected options if it is equal then set Select All checkbox checked otherwise unchecked it.

How do you uncheck a checkbox?

Once the checkbox is selected, we are calling prop() function as prop( "checked", true ) to check the checkbox and prop( "checked", false ) to uncheck the checkbox.

How do you uncheck select all checkbox when any one of their child got deselected?

Basically on click of any checkbox, if the "checked" prop is false, uncheck the Select All Checkbox. $("input[type=checkbox]").


2 Answers

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

like image 161
billyonecan Avatar answered Sep 24 '22 06:09

billyonecan


you could use class for all your checkboxes, and do:

$(".check_class").click(function() {   $(".check_class").attr("checked", false); //uncheck all checkboxes   $(this).attr("checked", true);  //check the clicked one }); 
like image 40
Sudhir Bastakoti Avatar answered Sep 24 '22 06:09

Sudhir Bastakoti