2

I am trying to reset the focus to the barcode field after a bar code has been entered with a tab as its last character (testing this manually right now). The actionFunction is being called, but the focus is not back on the bar code field after a bar code is entered; the focus is on the field below it. Can someone help me figure this out? Thanks.

<apex:page standardController="Contact" extensions="PhoneAThonScanController" title="Phone-A-Thon Entry">
<apex:form >
<apex:pageMessages id="msgs"/>

    <script language="javascript" type="text/javascript">
        function stopRKey(evt) {
           var evt = (evt) ? evt : ((event) ? event : null);
           var node = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null);
           if ((evt.keyCode == 13) && (node.type=="text")) {return false;}
        }

        document.onkeypress = stopRKey; 
    </script>

    <apex:actionFunction name="resetFocus" focus="barCode" action="{!scanBarCode}" reRender="block,cons,msgs" />

    <apex:pageBlock title="Inventory Item Receipt" id="block">
          <apex:pageBlockButtons >
              <apex:commandButton action="{!Cancel}" value="Cancel" 
                  onclick="return confirm('Unsaved contacts will be lost.  Are you sure you want to cancel?');"
                  immediate="true" rendered="{!contacts.size > 0}"/>
              <apex:commandButton action="{!cancel}" value="Cancel" immediate="true" rendered="{!contacts.size = 0}"/>
              <apex:commandButton action="{!save}" value="Save" rendered="{!contacts.size > 0}"/>
          </apex:pageblockbuttons>
          <apex:outputPanel id="panel" > 
            <apex:outputText value="Bar Code  " rendered="{!readyToScan}" />
            <apex:inputText value="{!barCode}" rendered="{!readyToScan}" onchange="resetFocus()" id="barCode"/>
     <!--           <apex:actionSupport event="onchange" action="{!scanBarCode}" rerender="cons,msgs,block"/>  -->

            </apex:inputText>
          </apex:outputPanel>
          <br></br>
          <br></br>

          <apex:repeat value="{!$ObjectType.Contact.FieldSets.Contact_Scan_Fieldset}" var="field">
              <apex:outputText value="{!field.label}  " />
              <apex:inputField value="{!newContact[field]}" >
                  <apex:actionSupport event="onchange" action="{!checkContact}" />
              </apex:inputField>
              <br></br>
          </apex:repeat> 
          <br></br>
          <br></br>

          <apex:outputText value="Ready To Scan  " />
          <apex:inputCheckbox value="{!readyToScan}" >
              <apex:actionSupport event="onchange" rerender="panel" />
          </apex:inputCheckbox>
     </apex:pageBlock>

     <apex:outputPanel id="cons" >
     <apex:pageBlock rendered="{!contacts.size > 0}">
        <apex:pageBlockTable value="{!contacts}" var="con" >
            <apex:facet name="header">
                <apex:outputText value="Contacts Added To The Batch" />
            </apex:facet>

            <apex:column >
                <apex:outputField value="{!con.Bar_Code__c}" />
            </apex:column>
            <apex:column >
                <apex:outputField value="{!con.firstName}" />
            </apex:column>
            <apex:column >
                <apex:outputField value="{!con.lastName}" />
            </apex:column>
        </apex:pageBlockTable>
     </apex:pageBlock>
     </apex:outputPanel>

</apex:form>
</apex:page>
6
  • 1
    First thing to try would be to put id="barCode" on the field you want the focus to be on rather than the surrounding panel. Commented Apr 6, 2014 at 16:00
  • Thanks, Keith! I made that change(see revised code above), and that fixed it. Commented Apr 6, 2014 at 16:10
  • 1
    Just type it into the "Your Answer" box below and "Post Your Answer" and then click the tick to accept it. Commented Apr 6, 2014 at 19:55
  • Also click on the tick mark then people know you are not still looking for an answer. Commented Apr 6, 2014 at 20:56
  • It won't let me click on it for 2 days. Commented Apr 6, 2014 at 21:20

1 Answer 1

2

First thing to try would be to put id="barCode" on the field you want the focus to be on rather than the surrounding panel.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.