function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
QuickDEVQuickDEV 

passing the <apex:selectRadio> value to JavaScript Function

Hi,
 
I have <apex:selectRadio> tag in my VisualForce page with  two radio buttons in it. 
I need to pass the value of the selected radio button to a JavaScript function to carry out further functionality.
Here is what I am trying to do:

// partial code from Controller

public List<SelectOption> getStateCountry() {
        List<SelectOption> nameCode= new List<SelectOption>(); 
        nameCode.add(new SelectOption('state','state')); 
        nameCode.add(new SelectOption('Country','Country')); 
      
        return nameCode; 
    }

 public String getNameCodeValue() {
        return nameCodeValue;
    }                 
    public void setNameCodeValue(String nameCodeValue) { this.nameCodeValue = nameCodeValue; }
  
 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++


// partial code from VisualForce page
<script> 
  function testFunction(radval) 
    {
      alert('in Testfunction  : '+ radval);
  
    }
 </script> 

      <apex:form >
                 <apex:selectRadio id="nameCodeID" value="{!nameCodeValue}" onchange="testFunction('{!nameCodeValue}')" >
                <apex:selectOptions value="{!nameCode}"/>
            </apex:selectRadio>
   </apex:form >

   ============

  Alert says:  "in Testfunction  : undefined"

  Am I going in the right direction, please help.

  Thanks

Best Answer chosen by Admin (Salesforce Developers) 
mshelmanmshelman

<script> function testFunction(therad) { alert('in Testfunction : ' + therad.value); } </script> <apex:form > <apex:selectRadio id="nameCodeID" value="{!nameCodeValue}" onchange="testFunction(this)" > <apex:selectOptions value="{!nameCode}"/> </apex:selectRadio> </apex:form >

 

All Answers

QuickDEVQuickDEV

I Apologize  for the errors in above code.

 

Correct version:

 

 

// partial code from Controller

public List<SelectOption> getnameCode() {
        List<SelectOption> nameCode= new List<SelectOption>(); 
        nameCode.add(new SelectOption('name','name')); 
        nameCode.add(new SelectOption('code','code')); 
      
        return nameCode; 
    }

 public String getNameCodeValue() {
        return nameCodeValue;
    }                 
    public void setNameCodeValue(String nameCodeValue) { this.nameCodeValue = nameCodeValue; }
  
 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++


// partial code from VisualForce page
<script> 
  function testFunction(radval) 
    {
      alert('in Testfunction  : '+ radval);
  
    }
 </script> 

      <apex:form >
            <apex:outputLabel value="Search By:"/> 
            <apex:selectRadio id="nameCodeID" value="{!nameCodeValue}" onchange="testFunction('{!nameCodeValue}')" >
                <apex:selectOptions value="{!nameCode}"/>
            </apex:selectRadio>
   </apex:form > 

QuickDEVQuickDEV

Help please....tried all ways I could

 

 

mshelmanmshelman

<script> function testFunction(therad) { alert('in Testfunction : ' + therad.value); } </script> <apex:form > <apex:selectRadio id="nameCodeID" value="{!nameCodeValue}" onchange="testFunction(this)" > <apex:selectOptions value="{!nameCode}"/> </apex:selectRadio> </apex:form >

 

This was selected as the best answer
QuickDEVQuickDEV
Thanks a bunch, mshelman.  It worked.
Balakrishna RaoBalakrishna Rao
apex:selectRadio id="nameCodeID" value="{!nameCodeValue}" onchange="testFunction(this)" > <apex:selectOptions value="{!nameCode}"/> </apex:selectRadio> </apex:form >
 
is there anyway to get the value of selected radio button value without using 'this' keyword.

in jquery can we get the value using Id = 'nameCodeID'  ?
Adeesh maddukuriAdeesh maddukuri
<script> function testFunction(therad) { alert('in Testfunction : ' + therad.value); } </script> <apex:form > <apex:selectRadio id="nameCodeID" value="{!nameCodeValue}" onchange="testFunction(this)" > <apex:selectOptions value="{!nameCode}"/> </apex:selectRadio> </apex:form