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
Chitral ChaddaChitral Chadda 

dynamic radio button selection on some condition


Request please assisst i m stuck on this work fir 2 weeks.

please

I have a group of 3 radio buttons. I want to dynamically check one of the radio button based on some condition. How do I make this work.
<input type="radio" name="optionsRadios" id="" value="option2" checked="if((account.Phone_Type_1__c==account.Preferred_Contact_Type__c),true,false)"
<input type="radio" name="optionsRadios" id="" value="option2" checked="if((account.Phone_Type_2__c==account.Preferred_Contact_Type__c),true,false)"
 <input type="radio" name="optionsRadios" id="" value="option2" checked="if((account.Phone_Type_3__c==account.Preferred_Contact_Type__c),true,false)"


Preferred_Contact_Type__c is a text field
Phone_Type_1__c
Phone_Type_2__c
Phone_Type_3__c

these all r pick list fields with value work , home ,fax
and user enters value in  Preferred_Contact_Type__c
which if matched should select desired radio button

in this only the last radio button is selected 
i want it to be preselected based on condition

 
Gil GourévitchGil Gourévitch
Hi,
your conditions must use the visualforce expressions syntax ( use {!expression} ), and try using checked="checked" instead of checked="true" :
<input 
	type="radio" 
	name="optionsRadios" 
	id="" 
	value="option1" 
	{!IF((account.Phone_Type_1__c==account.Preferred_Contact_Type__c),'checked="checked"', '')}
/>
Be sure that the values of fields Phone_Type_1__c and Preferred_Contact_Type__c match exactly.

Hope this helps
Gil

Question Solved ? Please mark as the best answer to help other users
Chitral ChaddaChitral Chadda

hi gil
i tried it, i m getting this error at the start of this line
 error :Element type "input" must be followed by either attribute specifications, ">" or "/>"

<input  type="radio" name="optionsRadios"  id="" value="option1"  {!IF((account.Phone_Type_1__c==account.Preferred_Contact_Type__c),'checked="checked"', '')}/>

something is wrong in this
Gil GourévitchGil Gourévitch
Hi,
Sorry, I did not test by myself...
You can use jquery in the VF page :
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
            <script type="text/javascript">
                $j = jQuery.noConflict();
            	$j(document).ready(function(){
                    switch("{!account.Preferred_Contact_Type__c}"){
                        case "{!account.Phone_Type_1__c}":
                            $j("#radio-option1").attr('checked', 'true');
                            break;
                        case "{!account.Phone_Type_2__c}":
                            $j("#radio-option2").attr('checked', 'true');
                            break;
                        //......
                    }
                });
            </script>

//.....more custom code...

<input type="radio" name="optionsRadios" id="radio-option1" />
<input type="radio" name="optionsRadios" id="radio-option2" />
The values of account.Preferred_Contact_Type__c and account.Phone_Type_X__c must match.

This time i tried it, it works for me ;-)

Hope this helps
Gil

Question Solved ? Please mark as the best answer to help other users !
 
Chitral ChaddaChitral Chadda
Thikyou gil It worked perfect !! Gil , is it possible in this that as one of the radio button appears selected i want that the other two radio button shud appear disabled /inactive . I have an edit button only when user clicks edit button then only all 3 radio buutons shud b enabled again. Since i have no idea about javascript So would be helpful of u help me out Thankss a ton God bless u
Gil GourévitchGil Gourévitch
Hi,
You could try this :
$j(document).ready(function(){
    $j('input[type="radio"]').attr('disabled', 'true');
    switch("{!account.Preferred_Contact_Type__c}"){
        case "{!account.Phone_Type_1__c}":
            $j("#radio-option1").attr('disabled', 'false');
            $j("#radio-option1").attr('checked', 'true');
            break;
        case "{!account.Phone_Type_2__c}":
            $j("#radio-option2").attr('disabled', 'false');
            $j("#radio-option2").attr('checked', 'true');
            break;
        //......
    }
});
This will disable all buttons but the one checked

Hope this helps
Gil

Question Solved ? Please mark as the best answer to help other users !
 
Chitral ChaddaChitral Chadda
hi gil thnku so much , one last thing wat happens is all radio butoon appear appear disabled including the one which is pre-selected. now i want that wen i click "request for change" button then the radio buttons shud be enabled again all the 3 buttons( for change/edit) this is section of code. this is controller public void Editpanel () { this.showHide=true; this.showField=false; this.requestButton=false; this.showButton=true; } is there any way round thru java script ?
Gil GourévitchGil Gourévitch
Hi,
to re-enable one radio button, you an use this :
$j("#radio-option2").attr('disabled', null);
instead of :
$j("#radio-option1").attr('disabled', 'false');
For your button, you can do this :
put this in the <script> section of your code : 
function enableRadioButtons(){
    $j('input[type="radio"]').attr('disabled', null);
}

//------
the button : 
<button onclick="enableRadioButtons()">request for change</button>
Hope this helps
Gil

Question Solved ? Please mark as the best answer to help other users
Chitral ChaddaChitral Chadda
hi,Gil i am again fighthing through only lil mistake is there , i cant figure cz i hv no idea using JS. when i try this only the pre selected button is disabled while other two are enabled also,when i click "request for change" button the same preselected button remains disabled while the other two remain enabled. what i require is that initially all 3 buttons should be disabled , including the one which is pre selected. only when the user clicks "request for change" button only then all the 3 radio buttons should be enabled(edit mode) , all the 3 buttons including preselected
Gil GourévitchGil Gourévitch
Hi,
Can you post the code you use ?
You first asked to initially disable all buttons that are not selected, I think that the above code do the trick.
Maybe you can clarify your need, so I can give you the right code.
Chitral ChaddaChitral Chadda

heres it gil ,sorry i dont know what went wrong i posted the code before heres it.

<apex:commandButton action="{!Editpanel}"  onclick="enableRadioButtons()" value="Request For Change" rendered="{!requestButton}" style="font-size:medium;margin-left: 45%;margin-right: 53%;">



<table class="table">
*********************************your code*************************************************
  <script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
            <script type="text/javascript">

              
$j = jQuery.noConflict();
                $j(document).ready(function()
                {  $j('input[type="radio"]').attr('disabled', true);           
                switch("{!account.Preferred_Contact_Type__c}")
                {
                        case "{!account.Phone_Type_1__c}":
                            $j("#radio-option1").attr('disabled', 'true');
                            $j("#radio-option1").attr('checked', 'true');
                            break;
                        case "{!account.Phone_Type_2__c}":
                            $j("#radio-option2").attr('disabled', 'true');
                            $j("#radio-option2").attr('checked', 'true');
                            break;
                        case "{!account.Phone_Type_3__c}":
                            $j("#radio-option3").attr('disabled', 'true');
                            $j("#radio-option3").attr('checked', 'true');
                            break;

                  }
                });
    function enableRadioButtons()
      {
        $j('input[type="radio"]').attr('disabled', null); 
         
      }
   
    </script>
************************************************************************************************
   <tbody>
      <tr>
         <td><apex:inputField value="{! account.Phone_Type_1__c}" rendered="{!!showField}" />
         <apex:outputField value="{! account.Phone_Type_1__c}" rendered="{!showField}"/>
        </td>

         <td><apex:inputText value="{! account.Phone_1__c}" rendered="{!!showField}"/>
         <apex:outputField value="{!account.Phone_1__c}"  rendered="{!showField}" style="margin-left: 30%"></apex:outputField>
         </td>
         <td>
         <label>  
         <input type="radio" name="optionsRadios" id="radio-option1" />Preferred     
          </label>
          </td>
            </tr>
      
        <tr>
         <td>
        <apex:inputField value="{! account.Phone_Type_2__c}"  rendered="{!!showField}" />
        <apex:outputField value="{! account.Phone_Type_2__c}" rendered="{!showField}"/>
        </td>

         <td><apex:inputText value="{! account.Phone_2__c}"  rendered="{!!showField}"/>
         <apex:outputField value="{!account.Phone_2__c}"  rendered="{!showField}" style="margin-left: 30%"></apex:outputField></td>
         <td><label>
          
      <input type="radio" name="optionsRadios" id="radio-option2" />Preferred
      </label></td>
      </tr>

         <tr>
         <td>
        <apex:inputField value="{! account.Phone_Type_3__c}" rendered="{!!showField}"/>
        <apex:outputField value="{! account.Phone_Type_3__c}" rendered="{!showField}"/>
        </td>
    
         <td><apex:inputText value="{! account.Phone_3__c}" rendered="{!!showField}"/>
         <apex:outputField value="{!account.Phone_3__c}"  rendered="{!showField}" style="margin-left: 30%"></apex:outputField></td>
         <td>
         <label>
         <input type="radio" name="optionsRadios" id="radio-option3" />Preferred
         </label></td>
            </tr>
 
</tbody>
</table>
with this code of java script you told .

all 3 radio buttons are disabled initially but when i click request for change then also all 3 radio buttons are disabled.
i requrie wen i click request for change button then all 3 radio button shud be enabled again.

Thanks



 
Gil GourévitchGil Gourévitch
Hi,
What happens here is that the commandbutton tag executes an AJAX call (a server call), an it refreshes the page. So event if the javascript code enables the radio buttons, you cannot see it, because the page refreshes.
To avoid this, you must use the rerender mecanism in visualforce : an AJAX call does not refresh the entire page, but only a portion.

The EditPanel method, should be defined as the following :
public PageReference Editpanel () {
this.showHide=true;
this.showField=false;
this.requestButton=false;
this.showButton=true;
return null;
}
The button, should look like that :
<apex:commandButton action="{!Editpanel}" oncomplete="enableRadioButtons()" value="Request For Change" rendered="{!requestButton}" rerender="panel" style="font-size:medium;margin-left: 45%;margin-right: 53%;">
You have to add some tag to the page :
// add an <apex:outputPanel> around the <table> tag with the panel id : 
<apex:outputPanel id="panel">
<table>
...
</table>
</apex:outputPanel>

Hope this helps
Gil

Question Solved ? Please mark as the best answer to help other users
Chitral ChaddaChitral Chadda
When I click request for change button nothing is happeng . earlier when I used to click on request fr change button it performed some action( like it opened fields for edit) now when I click on request fr change button nothing happens

Contact Information

Preferred
Preferred
Preferred
public pagereference Editpanel () { this.showHide=true; this.showField=false; this.requestButton=false; this.showButton=true; return null; }
Gil GourévitchGil Gourévitch
Hi,
There is an error in the syntax of the commandbutton (add closing </apex:commandButton> tag) :
<apex:commandButton action="{!Editpanel}"  onclick="enableRadioButtons()" value="Request For Change" rendered="{!requestButton}" style="font-size:medium;margin-left: 45%;margin-right: 53%;"></apex:commandButton>
There is another thing wich could explain your problem : in the switch statement, the "disabled" attribute must be "false" :
case "{!account.Phone_Type_1__c}":
    $j("#radio-option1").attr('disabled', 'false');
    $j("#radio-option1").attr('checked', 'true');
    break;

Hope this helps
Gil

Question Solved ? Please mark as the best answer to help other users
Chitral ChaddaChitral Chadda
Hey Gil Cud u pls drop me an email cse10315.sbit@gmail.com
Merlin ShanleyMerlin Shanley
Refer to more https://internetradiohoren.de/ sites listen to unlimited high-quality online radio stations on the internet