I have a VisualForce page where I am using SelectList and SelectOption. For example if my SelectOption list contians Option 1 Option 2 Option 3

I need to remove Option 2 on the page by its value not by index. I can add validation in my controller, but I wanted to do it on the page since I plan on reusing the selectlist.

<apex:selectList id="recordTypeSelect" value="{!currentCaseObject.RecordTypeId}"  >
     <apex:selectOptions value="{!newVisaTypesList}" id="visaoptionid"/>                                                                                                                      
  • You can add logic in the controller in the get method of your list – Liron C Mar 11 '17 at 14:08
  • Why don't you just build the list without that value in the first place? – Adrian Larson Mar 11 '17 at 14:18
  • I am not adding the values in the list manually. They come from a query. They are different for different times. But I want to be able to remove specific values. Not from the controller but from the page – Afroz Kazi Mar 11 '17 at 15:13
  • Any reasons behind this kind of design? What are the benefits of doing this, which can be better be maintained at controller – Santanu Boral Mar 11 '17 at 17:22
  • If it's action in the page (e.g. pressing button or selecting other value) it can invoke function in the controller that will reset the selection option list similarity to the code I added... . I'm not sure if it's possible only from the page without invoking the controller. Assume anything is possible but you will need to store/populate the values in other way instead of list variable in the controller. – Liron C Mar 11 '17 at 17:46

So I tried this in the page and it worked.

var x= document.getElementById("page:inputForm:recordTypeSelect");
for (var i=0; i<x.length; i++){
if(x.options[i].text == 'Value to be removed')

You can add logic in the controller in the get method of your list .


public List<SelectOption> newVisaTypesList{
       list<SelectOption> pOptions = new list<SelectOption>();  
        pOptions.add(new SelectOption('1', '1')); 
            pOptions.add(new SelectOption('2', '2')); 
        return pOptions;          
  • I want to do that on page itself without disturbing the controller. – Afroz Kazi Mar 11 '17 at 15:12

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.