I’m using MVC, and I’ve got a simple radio button setup:
<%=Html.RadioButton('my_flag', True)%><label>Yes</label> <%=Html.RadioButton('my_flag', False)%><label>No</label>
The only thing I’m missing is that you can’t click the label to select the radio button. Normally you’d use:
<label for='my_flag'>
but that associates both labels with the last radio button. Is there any way to associate the labels with the correct radio button?
Note: This is mimicking a paper form, so switching to a checkbox is not an option.
You need to give the two radio buttons the same name (so that they’re in the same group), but different ids (so that you can associate the label with each one individually). I’m not familiar with ASP.NET MVC, so I don’t know how to actually accomplish this, but that’s the solution.
Edit: a second possibility is to wrap the radio buttons inside the label tag, like so:
* Note that this way may actually have better browser compatibility, I know some browsers are still iffy about the name/id distinction