I would like a select option to appear in the page like this:

User and 2 "Reports To" Levels

but have not figured out how to escape the double quotes in a Visualforce attribute such as:

<apex:selectOption itemValue="2" itemLabel="User and 2 "Reports To" Levels"/>

How do I do this (without resorting to using a single quote instead)?


This can be done using the itemEscaped="false" attribute on the apex:selectOption tag. This will allow you to use HTML in the label text and render it into the page.

In your case, you want to use &quot; to produce the quote character in the label: User and 2 &quot;Reports To&quot; Levels.

<apex:selectList id="myElement" value="{!selectedValue}" size="1">
    <apex:selectOption itemEscaped="false" itemValue="2" 
        itemLabel="User and 2 &quot;Reports To&quot; Levels"/>

As a best practice, any hard coded text in Visualforce (or displayed through Visualforce / Apex) should be put in a Custom Label. (Setup->Create->Custom Labels). This allows developers, admins, et al to easily change the text if need be. This also helps support any translation needs.

Therefore, you could put the text, with quotes, in a custom label and reference it in your Visualforce page using a merge field like below:

 <apex:selectOption itemValue="2" itemLabel="{!$Label.UserReportsToLevels}"/>

Another option is to construct a list of select options in your controller, with the quoted text. See the controller in this example for details: http://www.salesforce.com/us/developer/docs/pages/Content/pages_compref_selectList.htm You could use labels for this option too.

  • or custom setting :-) depending on whats better for the use case – SalesForceGirl Jan 29 '14 at 19:33
  • I suggest "best practice" here needs some qualification. Using custom labels is appropriate if it is pretty likely that text changes and translation are going to happen. For many (most?) apps using them would needlessly add complexity and cost. – Keith C Jan 29 '14 at 21:44
  • 4
    @KeithC I would definitely argue against that. It has been best practice in application development for years to make your applications i18n friendly. Visualforce is no different. – Jesse Altman Jan 30 '14 at 16:28
  • @JesseAltman So YAGNI never applies? – Keith C Jan 30 '14 at 18:06
  • 1
    @KeithC that is definitely a good point, but I would say in this instance it really doesn't. In my mind, this doesn't add any complexity, just moves information slightly to make it easier to support i18n. – Jesse Altman Jan 30 '14 at 18:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.