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
michaelforcemichaelforce 

(Bug?) Lookup filters + partial re-render = cursor jump

I've come across a very specific but easily reproduceable bug.  What happens is that if you have a form on a Visualforce Page with some inputFields on it which include two lookup fields, one dependent on the other via lookup filtering, and you do a partial rerender of the page, the cursor will jump from whatever field it is on up to the first field on the form.

I know, I know... that's pretty specific.  But several of my customers have hit this.  So I am looking for confirmation that it is a bug, and more importantly I am looking for advice on how I can combat this.

How to Reproduce:

First create two lookup fields on the Account object.  One lookup to the Account object and one to the Contact object.  The lookup to the Account object can be named "Distributor__c" and the one to the Contact "Distributor_Contact__c".  Then on the Contact lookup field, add the following lookup filter:

User-added image

The create this Apex Class...

public with sharing class cursorJumpExtension {

    public Integer refreshCount {get;set;}

    public cursorJumpExtension(ApexPages.StandardController controller) {
        refreshCount=0;
    }

    public void refreshPlus(){
        refreshCount+=1;
    }

}

Then this Visualforce Page...
 
<apex:page standardController="Account" extensions="cursorJumpExtension">
<apex:form >

    <apex:actionFunction name="doRefresh" action="{!refreshPlus}" reRender="refreshing" immediate="true"/>

    <apex:pageBlock id="refreshing">
   
        <script>
            window.setTimeout(doRefresh,3000);   
        </script>
   
        This block re-renders every 3 seconds.<br/><br/>
        Last refresh at: {!NOW()}<br/>
        Number of times refreshed: {!refreshCount}
    </apex:pageBlock>

    <apex:pageBlock >
        <apex:pageBlockSection columns="1">
            <apex:inputField value="{!Account.Name}"/>
            <apex:inputField value="{!Account.Distributor__c}"/>
            <apex:inputField value="{!Account.Distributor_Contact__c}"/>
            <apex:inputField value="{!Account.website}"/>
        </apex:pageBlockSection>
    </apex:pageBlock>

</apex:form>
</apex:page>


Now navigate to the Page and put the cursor in the website field.  You will see that the next time the top box refreshes, the cursor jumps to the first field!  Annoying!  Then you can go back to your lookup field and deactivate the lookup filter... OR simply change it to a different kind of filter that doesn't reference another field, and the cursor jumping phenomena stops.

Anyone know what I can do here??
Vamsi KrishnaVamsi Krishna
Michael,
I don't think its specific to visualforce.. i have seen this behaviour with ASP.NET and AJAX calls as well..
usually everytime we do partial rendering of a page, when the rendering completes, the focus shifts from the last focus element..
as a workaround we use to get the last active element (https://developer.mozilla.org/en-US/docs/Web/API/document.activeElement) and then restore the focus to that again..
sridhar v 7sridhar v 7
Michael,
Were you able to get around this issue at all?

We are facing the same issue in our page? any suggestions on this would be really helpful.


Thanks,
Sridhar