1

I am using an input tag in my visualforce page with type="Radio" and value="{!Selected}".

In the controller, I have declared the boolean "Selected" with get;set;. But when i select the radio button, the variable "selected" does not get updaated to true in controller.

I could not use selectoptions due to my requirement.

Should a different approach be followed to achieve the same

1
  • Is it possible to set a value to a controller variable through javascript? I mean in this case, can we set selected variable to true in the on click event in the input tag? Commented Mar 31, 2015 at 14:13

3 Answers 3

1

You have multiply ways to achieve it:

  • Use apex:selectRadio tag instead of html-input. You will then get the value of the variable into controller automatically. The Visualforce will do this job for you.
  • You can still use html-input but then you should implement a structure that will take this value to controller. For example apex:actionFunctiontogether with apex:param.

Example:

<apex:selectList id="chooseColor" value="{!Selected}" size="1">
    <apex:selectOption itemValue="1" itemLabel="Option 1"/>
    <apex:selectOption itemValue="2" itemLabel="Option 2"/>
</apex:selectList>
3
  • Thanks but I cannot use selectoption as i need to disable/enable the radio buttons based on conditions. Commented Mar 31, 2015 at 14:07
  • @SFDCLearner Is it a reason why you can not use apex:selectOption??? What is with javascript and/or itemDisabled parameter? Commented Mar 31, 2015 at 14:56
  • Thanks. I tried using apex:selectoption. I tried to disable them via jquery but it did not work for select option radio buttons alone. Though it worked for other fields. i will try disabling through boolean in itemdisabled attribute. Commented Mar 31, 2015 at 17:02
0

As mast0r mentioned, you can use apex:ActionFunction and Javascript to achieve this.

This example should help: http://www.salesforceworld.blogspot.com/2011/06/parameter-passing-using.html#!http://salesforceworld.blogspot.com/2011/06/parameter-passing-using.html

You create an ActionFunction wrapped around your radio button (let's call it <apex:actionFunction name="someActionFunctionName"). Create a Javascript function that takes in one parameter (let's say for example, myRadioElement) and calls the name parameter of the actionFunction, then passes in the value of your HTML element as the parameter, e.g. someActionFunctionName(document.getElementByID(myRadioElement)). You can then call the Javascript function on whatever event you want on the element, and the value can be received and accessed in your controller like:

boolean interviewdate=Apexpages.currentPage().getParameters().get('myRadioElement');
0
  <apex:selectRadio value="{!selected}">
         <apex:selectOption itemValue="true" itemLabel="First"/>
         <apex:selectOption itemValue="false" itemLabel="Second"/>
         <apex:selectOption itemValue="true" itemLabel="Third"/>
        **<apex:actionSupport event="onchange" action="{!testSelected}"  rerender="out" />**
     </apex:selectRadio>


    <apex:outputPanel id="out"> 
         <p>You have selected:</p> 
         <apex:outputText value="{!selected}"/> 
   </apex:outputPanel> 

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .