I have a visual force page which dynamically displays multiple records with check box's. I need to allow the user to select only one check box at a time.

I know it is possible with only javascript. Can any one post sample code for this? If you have any other suggestions as well, please let me know.

  • 5
    You can use radio button instead of checkbox Apr 17 '15 at 15:32
  • I tried that as well, but they were not populating correctly because the records are populating dynamically. Apr 17 '15 at 15:41
  • 3
    Try posting your attempt at doing this and ask for any other suggestions as well for it. Usually, this site requests that Users produce a specific attempt at coding their problem along with the question. Alternatively, if you are asking for different ways on how to do this, you could post that as a separate question. Something along the lines of: I know that I can use javascript to enforce only a single checkbox checked at a time, but is there any other ways to do so as well? Please produce a list and short description if possible.
    – dphil
    Apr 17 '15 at 16:58

There are many ways. But the best solution is to get it working with checkboxes. Other ways are,

1 - Write page level javascript to ensure that only one checkbox is checked. Get element by class, iterate over it and ensure that only one is checked. This javascript method should be called whenever user click on checkbox.

2 - Write actionfunction to check that only one field is checked using a controller function. Call it whenever user clicks checkbox.

  • 1
    or option 3 - onchange event with actionSupport to controller that unchecks all other boxes and then returns null to refresh page (or actionRegion); I use a variation of #1 with jQuery
    – cropredy
    Apr 17 '15 at 18:40

I'm a little late to the party on this one but I had the same issue and could not find a complete solution, so I came up with my own and made a blog post about it here

The solution uses a couple of lines of jQuery, but is fairly straightforward.

Hopefully that post will help anyone having the same issue in the future.

  • Not too late for my party, Chris. Thanks for making this blog as I put this issue on the 'back-burner'. I'll try it out and get back to you how it works. Thanks again! May 12 '15 at 15:16
  • No problem, glad I could help. May 13 '15 at 15:25

Not the answer you're looking for? Browse other questions tagged or ask your own question.