[SalesForce] Display Radio buttons group in visualforce page

I have two objects Question__c and Answer__c. For every question record there will be 4 answer records. I need to have 4 answers as radio buttons for every question.
How to display the answers as radio group for every question.

 <apex:pageBlock title="Questions">
 <apex:repeat value="{!questions}" var="q">
    <br/>

        <apex:outputField value="{!q.Question__c}"/>
          <apex:pageBlockTable value="{!q.Answers__r}" var="a">

            <apex:column >
         <apex:selectradio layout="lineDirection"> 
              <apex:selectOption itemValue="{!a.Answer__c }" itemLabel="{!a.Answer__c }">  </apex:selectOption>
         </apex:selectradio>
     </apex:column>

    </apex:pageBlockTable>

 </apex:repeat>

I tried like this but, radio buttons aren't grouped. How to make these buttons grouped?

Best Answer

I had to use some JavaScript and HTML Input tag to work around this. This is extract from my code. So what this does is have an inputHidden in your code and set the value in this field in JavaScript onclick event.

<apex:pageBlockTable value="{!someList}" var="con">
<apex:column value="{!con.name}" />

<apex:column>
    <input type="radio" name="chosen" id="{!con.id}" VALUE="{!con.id}" onclick="changeValue(this,'{!$Component.RadioButtonValue}');"/>
</apex:column>    

</apex:pageBlockTable>

<apex:inputHidden value="{!contactId}" id="RadioButtonValue" />



<script>
function changeValue(input, textid) {
    document.getElementById(textid).value = input.value;
}   
</script>
Related Topic