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
suresh goudsuresh goud 

Disabling Picklist field by selecting radio button.

Hi,

I have Created a Radio Button with options 'YES' & 'NO' .   and created a Multi select picklist field below.

Now my requirement is
1. In Radio button, If YES is selected then picklist values on below has to be enable and give choice to select from the  multi picklist
2. In radio Button, If NO is selected then Picklist values has to be disable. 

Can any one help me to do this functionality. If so it would be helpfull to me.

Thanks,
Suresh Goud

Best Answer chosen by Admin (Salesforce Developers) 
Chamil MadusankaChamil Madusanka

Hi,

 

Try this code,

 

<apex:page controller="sampleCon">

<script type="text/javascript">
     
    function getActive(selectedValue) 
    {  
    if( selectedValue == 'YES')
    {
    var ele=document.getElementById('{!$Component.form1.dropdown}');
      ele.disabled =false;
    }else if(selectedValue == 'NO')
    {
         var ele=document.getElementById('{!$Component.form1.dropdown}');
      ele.disabled =true;
    }
      
            
    } 
    </script>
    <apex:form id="form1">
        <apex:selectRadio value="{!selectedValue}" layout="pageDirection" onchange="getActive(this.value);">
            <apex:selectOption itemValue="YES" itemLabel="YES"/>
            <apex:selectOption itemValue="NO" itemLabel="No"/>
            </apex:selectRadio><p/>
            
            <apex:selectList id="dropdown" size="1">
               <apex:selectOption itemValue="YES" itemLabel="YES"/>
            <apex:selectOption itemValue="NO" itemLabel="No"/>
            </apex:selectList>
            
  </apex:form>
</apex:page>

 

public class sampleCon {
    public String selectedValue{get;set;}
         
    
                
    
}

 If a reply to a post answers your question or resolves your problem, please mark it as the solution to the post so that others may benefit.

 

chamil's blog

All Answers

Kevin SwiggumKevin Swiggum

If the Yes/No option is a Picklist field type, you could use the built-in dependent picklists functionality. However, I'm not sure it would work if you're using radio buttons rather than the inputField control (which would appear as a dropdown list rather than a radio button)

 

If it's an option to make the Yes/No field a dropdown list rather than a radio button, then dependent picklist functionality should work without any custom coding.

Chamil MadusankaChamil Madusanka

Hi,

 

Try this code,

 

<apex:page controller="sampleCon">

<script type="text/javascript">
     
    function getActive(selectedValue) 
    {  
    if( selectedValue == 'YES')
    {
    var ele=document.getElementById('{!$Component.form1.dropdown}');
      ele.disabled =false;
    }else if(selectedValue == 'NO')
    {
         var ele=document.getElementById('{!$Component.form1.dropdown}');
      ele.disabled =true;
    }
      
            
    } 
    </script>
    <apex:form id="form1">
        <apex:selectRadio value="{!selectedValue}" layout="pageDirection" onchange="getActive(this.value);">
            <apex:selectOption itemValue="YES" itemLabel="YES"/>
            <apex:selectOption itemValue="NO" itemLabel="No"/>
            </apex:selectRadio><p/>
            
            <apex:selectList id="dropdown" size="1">
               <apex:selectOption itemValue="YES" itemLabel="YES"/>
            <apex:selectOption itemValue="NO" itemLabel="No"/>
            </apex:selectList>
            
  </apex:form>
</apex:page>

 

public class sampleCon {
    public String selectedValue{get;set;}
         
    
                
    
}

 If a reply to a post answers your question or resolves your problem, please mark it as the solution to the post so that others may benefit.

 

chamil's blog

This was selected as the best answer
suresh goudsuresh goud

Thank you.....Chamil

 

The code is working......awesome..

 

 

Suresh Goud

mmrrmmrr

I have a same kind of requirement for checkbox.

 

If checkbox is checked, I have to hide another field.  If checkbox is unchecked, I have to show another picklist field,

against if the picklist field value selected is "other" , I have to show one more field.  Can you please help me in getting this.


chamil wrote:

Hi,

 

Try this code,

 

<apex:page controller="sampleCon">

<script type="text/javascript">
     
    function getActive(selectedValue) 
    {  
    if( selectedValue == 'YES')
    {
    var ele=document.getElementById('{!$Component.form1.dropdown}');
      ele.disabled =false;
    }else if(selectedValue == 'NO')
    {
         var ele=document.getElementById('{!$Component.form1.dropdown}');
      ele.disabled =true;
    }
      
            
    } 
    </script>
    <apex:form id="form1">
        <apex:selectRadio value="{!selectedValue}" layout="pageDirection" onchange="getActive(this.value);">
            <apex:selectOption itemValue="YES" itemLabel="YES"/>
            <apex:selectOption itemValue="NO" itemLabel="No"/>
            </apex:selectRadio><p/>
            
            <apex:selectList id="dropdown" size="1">
               <apex:selectOption itemValue="YES" itemLabel="YES"/>
            <apex:selectOption itemValue="NO" itemLabel="No"/>
            </apex:selectList>
            
  </apex:form>
</apex:page>

 

public class sampleCon {
    public String selectedValue{get;set;}
         
    
                
    
}

 If a reply to a post answers your question or resolves your problem, please mark it as the solution to the post so that others may benefit.

 

chamil's blog



chamil wrote:

Hi,

 

Try this code,

 

<apex:page controller="sampleCon">

<script type="text/javascript">
     
    function getActive(selectedValue) 
    {  
    if( selectedValue == 'YES')
    {
    var ele=document.getElementById('{!$Component.form1.dropdown}');
      ele.disabled =false;
    }else if(selectedValue == 'NO')
    {
         var ele=document.getElementById('{!$Component.form1.dropdown}');
      ele.disabled =true;
    }
      
            
    } 
    </script>
    <apex:form id="form1">
        <apex:selectRadio value="{!selectedValue}" layout="pageDirection" onchange="getActive(this.value);">
            <apex:selectOption itemValue="YES" itemLabel="YES"/>
            <apex:selectOption itemValue="NO" itemLabel="No"/>
            </apex:selectRadio><p/>
            
            <apex:selectList id="dropdown" size="1">
               <apex:selectOption itemValue="YES" itemLabel="YES"/>
            <apex:selectOption itemValue="NO" itemLabel="No"/>
            </apex:selectList>
            
  </apex:form>
</apex:page>

 

public class sampleCon {
    public String selectedValue{get;set;}
         
    
                
    
}

 If a reply to a post answers your question or resolves your problem, please mark it as the solution to the post so that others may benefit.

 

chamil's blog




Nilang PandeyNilang Pandey
The last code also doesn't work. The fields are not getting disabled.