Is there a way to make checkboxes act like radio buttons? I assume this could be done with jQuery?
<input type="checkbox" class="radio" value="1" name="fooby[1][]" />
<input type="checkbox" class="radio" value="1" name="fooby[1][]" />
<input type="checkbox" class="radio" value="1" name="fooby[1][]" />
<input type="checkbox" class="radio" value="1" name="fooby[2][]" />
<input type="checkbox" class="radio" value="1" name="fooby[2][]" />
<input type="checkbox" class="radio" value="1" name="fooby[2][]" />
If one box was checked the others in the group would uncheck.
$("input:checkbox"). click(function(){ var group = "input:checkbox[name='"+$(this). attr("name")+"']"; $(group). attr("checked",false); $(this).
To make checkbox behave like radio buttons with JavaScript, we can listen to the body element's click listener. And in the listener, we uncheck all the checkboxes and the check off the one that matches the one that's clicked. to add the checkboxes.
Both are commonly used together on forms to select options from a list. However, a radio button is a circle with a dot inside, while a checkbox is a square with a checkmark inside—two different visual cues. Some might say that their functions are different, so they should look different.
$("input:checkbox").click(function(){
var group = "input:checkbox[name='"+$(this).attr("name")+"']";
$(group).attr("checked",false);
$(this).attr("checked",true);
});
This will do it, although i do agree this might be a bad idea.
Online example: http://jsfiddle.net/m5EuS/1/
UPDATE added group separation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With