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
LakshmanLakshman 

How to set default value in Select Option using javascript or in server side.

I am having the following Apex code in my class:

 

public List<SelectOption> getItems() {
                List<SelectOption> options = new List<SelectOption>();
                options.add(new SelectOption('US','US'));
                options.add(new SelectOption('CANADA','Canada'));
                options.add(new SelectOption('MEXICO','Mexico'));
                return options;
        }

The visual force page code which fetches all information is as below:

 

<apex:selectList size="1" id="countryList" onChange="setCountry(this.value);">
    <apex:selectOptions value="{!items}"/>
    </apex:selectList>

 

 

The default value that we get after executing this visual force page is US, but I want it to be suppose Canada, How can i do it.

Please Help.

 

Regards,

Lakshman

 

~Share Knowledge, grow more~

Best Answer chosen by Admin (Salesforce Developers) 
MiddhaMiddha

Hey Lakshman,

 

This can be done in APEX by setting the value in your variable you are using in Select Option. In your case you should create a new APEX String variable in your class:

 

 

public String selectedCountry
{
   get
    {
       if(selectedCountry==null)
           selectedCountry='CANADA';
    }
set;
}
public List<SelectOption> getItems() {
                List<SelectOption> options = new List<SelectOption>();
                options.add(new SelectOption('US','US'));
                options.add(new SelectOption('CANADA','Canada'));
                options.add(new SelectOption('MEXICO','Mexico'));
                return options;
        }

 

 

and your page should have SelectList as:

 

<apex:selectList size="1" id="countryList" onChange="setCountry(this.value);" value="{!selectedCountry}">
    <apex:selectOptions value="{!items}"/>
    </apex:selectList>

 

 

This should default your picklist value to Canada. if you want to do it using JS on VF page you can do it by:

 

document.getElementById('{!$Component.countryList}').value='Canada';

All Answers

MiddhaMiddha

Hey Lakshman,

 

This can be done in APEX by setting the value in your variable you are using in Select Option. In your case you should create a new APEX String variable in your class:

 

 

public String selectedCountry
{
   get
    {
       if(selectedCountry==null)
           selectedCountry='CANADA';
    }
set;
}
public List<SelectOption> getItems() {
                List<SelectOption> options = new List<SelectOption>();
                options.add(new SelectOption('US','US'));
                options.add(new SelectOption('CANADA','Canada'));
                options.add(new SelectOption('MEXICO','Mexico'));
                return options;
        }

 

 

and your page should have SelectList as:

 

<apex:selectList size="1" id="countryList" onChange="setCountry(this.value);" value="{!selectedCountry}">
    <apex:selectOptions value="{!items}"/>
    </apex:selectList>

 

 

This should default your picklist value to Canada. if you want to do it using JS on VF page you can do it by:

 

document.getElementById('{!$Component.countryList}').value='Canada';

This was selected as the best answer
LakshmanLakshman

Thanks for your quick reply.

I think this will work, will implement and let you know if it works.

 

Regards,

Lakshman

 

~Share Knowledge, grow more~

LakshmanLakshman

Its working Cheers!!!

Thank you GreatG!

Manuel EcheverriaManuel Echeverria
Is the answer from Middha work with a multipick list? If the apex:select list have multiselect="true"????


Thanks