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

Set default value for dropdown
Hi,
I am using
<apex: selectoptions ={!listValue} id=picklist" />
here listValue is holding values 1,2,3,4,5 but i want to make 3 as defualt. How can I make it using javascript/jquery? Please help
I am using
<apex: selectoptions ={!listValue} id=picklist" />
here listValue is holding values 1,2,3,4,5 but i want to make 3 as defualt. How can I make it using javascript/jquery? Please help
Let me know if it helped.
Thanks
Ankit
Thanks for the answer. I want to display the default value. But in the code which u have sent, they are assigning the default value.
I have 5 values in my picklist 1,2,3,4,5---- while displaying to user 3 should be selected upon page load. if user clicks on dropdown he can still see 1,2,3,4,5
Thanks
<apex:page controller="SetValue">
<script type='text/javascript'>
$( document ).ready(function() {
$("#Default").val("3");
});
</script>
<apex:form >
<br/>
<apex:selectList size="1" title="Country" id="Default" >
<apex:selectOptions value="{!items}"/>
</apex:selectList>
</apex:form>
</apex:page>
But value 3 is not getting preselected in Dropdown.
Please suggest
The easiest approach is to set this in your controller's constructor. The idea is to use the value attribute on your select list, which will get and set a variable in your controller. For example:
Your visualforce example would look like:
Your constructor might look like:
Note that selectedItem would need to match the value of one of the select options in your items list.
Its working fine. Thanks
But i want to do it using jQuery/javascript. Is it possible?
Thanks
I made some changes in your script.
As apex:selectlist will create a Dom Element Id.
If You want to use jquery/javascript to do then use this.
<apex:page controller="SetValue">
<script type='text/javascript'>
$( document ).ready(function() {
$('select[id*="Default"]').val('3') ;
});
</script>
<apex:form >
<br/>
<apex:selectList size="1" title="Country" id="Default" >
<apex:selectOptions value="{!items}"/>
</apex:selectList>
</apex:form>
</apex:page>
I'm using your script on a Visualforce page that uses the standard controller for accounts - it is setting the value I want in the view dropdown, but the page isn't loading the content based on the selected view. Any suggestions? Thanks.