0

I am using selectList tag in my visualforce page, this selectList contains years number (2001,2002,...,2020) as follows:

<apex:selectList id="mySelect" styleClass="form_field flex_grow" required="true" multiselect="false" size="1" label="Type"  value="{!selectedYear}">
    <apex:selectOptions value="{!Years}"/>
</apex:selectList>

The values of year comes from a method from the controller (it is already populated). The first item appears on the selectList is 2001

What I want to do is when I click the selectList to display years values I want the focused value in the list not to be 2001 but 2010 ( once i click the selectList to display list of years i want the focused value to be 2010).

Here is the source code I have implemented but it doesn't work:

$('#mySelect').focus(function(){

    if($(this).data('focused') !==  true)
    {
        $(this).data('focused',true);  
        $(this).find('option[value="2010"]').prop('selected',true);
    }

});

$('#mySelect').blur(function(){
   $(this).data('focused',false); 
});

Thank you in advance

  • If this helped can you please mark my answer as the solution. – Dan Kowalski Oct 3 '17 at 18:41
0

Change the code jquery code to read like this.

jQuery('[id$=mySelect]').focus(function() {
    if($(this).data('focused') !==  true)
    {
        $(this).data('focused',true);  
        $(this).find('option[value="2010"]').prop('selected',true);
    }
});

jQuery('[id$=mySelect').blur(function(){
    $(this).data('focused',false); 
});

Also below is a link that will help with other jquery development needs. It might help with more standard tips and tricks then what I added. https://developer.salesforce.com/page/Developing_Apps_with_jQuery

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.