function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
msb-appsupport1.3905906418879758E12msb-appsupport1.3905906418879758E12 

How to check only one check box at a time using visualforce page?

Hi,
I am building a visualforce page associating with Apex code.
I have multiple checkboxes. And I want user to check only one checkbox at a time.

How could I do it?

I have tried some javascript associating with onclick feature, however, it doesn't work.

Thanks.
AkersAkers

Try using Radio buttons instead of Checkboxes, gives more flexibility. Still if you need checkboxes, try using the following javascript

 

<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][]" />

<script>
$("input:checkbox").click(function() {
    if ($(this).is(":checked")) {
        var group = "input:checkbox[name='" + $(this).attr("name") + "']";
        $(group).prop("checked", false);
        $(this).prop("checked", true);
    } else {
        $(this).prop("checked", false);
    }
});
</script>