+ Start a Discussion

input radio checked problems/bug? Is there a workaround?

Setting the checked attribute in a dynamic way doesn't seem possible.

As per html spec, checked="checked", checked = "junk", checked="" all lead to a checked radio button. Just the presence of attribute is enough.


So it leads to following problems. I guess the workaround would be in Javascript?




//sfdc is smart enought to removes checked attribute in the rendered html because it is empty
<input type="radio" name="r1" value="Bike" checked="" />

//SFDC renders checked="" in html, hence button is checked. SFDC should get rid attribute as it does above
 <input type="radio" name="r1" value="Bike" checked="{IF(false,'checked' ,'')}" />

//So try to get rid of the attribute itself dynamically. However SFDC does not allow the next two syntax in API 19.0 with complile time error - Element type "input" should be followed by either attribute specifications, ">" or "/>". Seems like it wants attribute=value syntax. It allows it at compile time in 16.0, but end users get the same message at run time, which is not acceptable.
 <input type="radio" name="r1" value="Bike" {!IF(false,'checked' ,'')} />
<input type="radio" name="r1" value="Bike" {!IF(false,'checked=\"checked\"','')} />







Best Answer chosen by Admin (Salesforce Developers) 

outputpanel did work.




All Answers




I can think of two possible options.


  1. Use the apex:selectRadio control instead of writing your own HTML.
  2. Place two instances of the input in different outputPanels, one which is checked and one which is not, and then make the rendered property of each outputPanel conditional on a value.






#2 might work. Let me try it.

#1 does not work because my radio buttons are in a table grid.





outputpanel did work.




This was selected as the best answer

Would you mind marking my post as the solution?




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

Preferred_Contact_Type__c is a text field

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

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
Daniel SokolowskiDaniel Sokolowski
This is pretty sad, and tyring to use `<apex:selectRadio value="{!bHideSearchBar}">`  does not help as you have to have the required wrapping clases; so best option:

<apex:outputText rendered="{!bHideSearchBar == true}">
                                <input type="radio" name="bHideSearchBar" value="true"  checked="checked" />Yes
                                <input type="radio" name="bHideSearchBar" value="false" checked="" />No
                            <apex:outputText rendered="{!bHideSearchBar == false}">
                                <input type="radio" name="bHideSearchBar" value="true" checked="" />Yes
                                <input type="radio" name="bHideSearchBar" value="false"  checked="checked" />No