You need to sign in to do that
Don't have an account?

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~
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:
and your page should have SelectList as:
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
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:
and your page should have SelectList as:
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';
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~
Its working Cheers!!!
Thank you GreatG!
Thanks