jQuery - Unchecking checkboxes that act like radio buttons

Posted by Cecil on Stack Overflow See other posts from Stack Overflow or by Cecil
Published on 2011-01-16T16:32:58Z Indexed on 2011/01/16 16:53 UTC
Read the original article Hit count: 220

Filed under:

Hey All,

I have the following jQuery code to make my checkboxes act like radio buttons, so that only 1 of the 3 can be checked at a time.

<script type="text/javascript" language="javascript">
$(document).ready(function() {
    $("#testing input:checkbox").change(function(){
        var checkname = $(this).attr("name");
        $("input:checkbox[name='" + checkname + "']").removeAttr("checked");
        this.checked = true;
    });
});
</script>

The checkboxes are layed out like the following:

<input type="checkbox" id="testing" name="testing" value="B">
<input type="checkbox" id="testing" name="testing" value="I">
<input type="checkbox" id="testing" name="testing" value="A">

This works exactly how i want it to work, not a problem, except once i click one of the 3, i cant unclick it so that none of them are checked, this is what i want to happen, so along with being only able to click one at a time, im able to uncheck them completely.

Any help would be grand :)

© Stack Overflow or respective owner

Related posts about jQuery