1

Two radio buttons are present with two options to select. User can select only one .

<div class="slds-col slds-size--1-of-3">   
    <input id="Quarterlyselector" type="radio" name="myRadio" value="Q" checked="checked" > Quarterly </input>                               
</div>
<div class="slds-col slds-size--1-of-2">
    <input id="Monthlyselector" type="radio" name="myRadio" value="M" > Monthly </input>
 </div>

What would be the syntax to assign the user selection to a variable , so that it can be passed on to apex as a method parameter?

  • check out this answer this will help you out. – Praveen May 14 '16 at 17:37
  • 3
    I agree that there already seems to be an answer to your question. – Adrian Larson May 14 '16 at 18:18
  • the example talks abou Param tag . I don't think I'll have option to use that in my code. Can anyone help me with JS syntax. Will it be something like Var option = myradio.value? – Vanilla_Sky May 14 '16 at 19:08
  • 1
    Why you couldn't use apex:param? you're not able to modify controller? – Ilya Lepesh May 14 '16 at 19:19
1

You can pass selected value to controller within apex:inputhidden.

In the following example commandbutton executes JS, which looks for radio element and assign its value to inputhidden component:

JS:

<script type="text/javascript">
function CaptureRadio() {
    var y = document.querySelectorAll('[id$="HiddenInput"]')[0];
    var radios = document.getElementsByName('myRadio');
    for (var i = 0, length = radios.length; i < length; i++) {
        if (radios[i].checked) {
            y.value = radios[i].value;
            break;
        }
    }
}
</script>

VF:

<apex:pageBlock >          
    <input type="radio" name="myRadio" value="Q" checked="checked" >  
        Quarterly 
    </input>
    <input type="radio" name="myRadio" value="M" >  
        Monthly 
    </input>
    <apex:commandButton value="Proceed" onclick="CaptureRadio();" 
        action="{!proceed}" rerender="theHiddenInput"/>
    <apex:inputHidden id="theHiddenInput" value="{!selected}" />
</apex:pageBlock>

Apex:

public string selected {get;set;}
public void proceed() {
    system.debug('selected: ' + selected);
}
  • Considering that the radio buttons are getting displayed using JS only and not apex , How can I assign the selected value to any variable? – Vanilla_Sky May 14 '16 at 19:41
  • @Vanilla_Sky The posted code has no dependency on page load DOM. The elements just have to exist when you click the button. – Adrian Larson May 14 '16 at 20:04
  • @AdrianLarson - Okay . Can I pass it as a parameter in my Remote Action method? – Vanilla_Sky May 16 '16 at 10:21

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