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
Ganesh HegdeGanesh Hegde 

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
Ankit AroraAnkit Arora
Here is the solution : https://developer.salesforce.com/forums/ForumsMain?id=906F00000008yttIAA

Let me know if it helped.

Thanks
Ankit
Ganesh HegdeGanesh Hegde
Hi,

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
Ganesh HegdeGanesh Hegde
I am doing like this --->

<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
James LoghryJames Loghry

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:

<apex:selectList size="1" title="Country" id="Default" value="{!selectedItem}">
    <apex:selectOptions value="{!items}"/>
</apex:selectList>


Your constructor might look like:

public String selectedItem {get; set;}

public MyController(){
    this.selectedItem = 'defaultValue';
}

Note that selectedItem would need to match the value of one of the select options in your items list.
 
Ganesh HegdeGanesh Hegde
Hi James,

Its working fine. Thanks
But i want to do it using jQuery/javascript. Is it possible?

Thanks
Dushyant SonwarDushyant Sonwar
Hi Ganesh,

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>
Dinavahi.Sivakumar SivakumarDinavahi.Sivakumar Sivakumar
HI I am new to salesforce I want to build an APP in salesforce can u please give me in detail steps 
Elizabeth1842Elizabeth1842
Hi Dushyant -

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.