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
CharlesDouCharlesDou 

Unable to set focus on visualforce page

Hi All,

I am a starter on visualforce development and I am having a problem setting focus using jQuery on visualforce page.

here is part of the code for the visualforce:

<apex:form id="dataform">
    <apex:pageBlock title="LABOR AND QUANTITY BOOKING" mode="edit" id="pageblock">
        <apex:pageBlockSection columns="1" id="section">
            <apex:inputText value="{!operator}" label="Operator" id="operator"/>
            <apex:inputText value="{!workOrderNumber}" label="Work Order" id="wo"/>
       </apex:pageBlockSection>
      <apex:pageBlockButtons id="buttons" location="bottom">
            <apex:commandButton value="Save" action="{!save}" id="save" />
            <apex:commandButton value="Cancel" action="{!cancel}"/>
       </apex:pageBlockButtons>
    </apex:pageBlock>
</apex:form>

What I am trying to do is to set focus on the second field(work order)when page loads, and here is the jQuey
<apex:includeScript value="{!URLFOR($Resource.jquery)}"  />
<script type="text/javascript">
    jQuery(document).ready(function(){
        jQuery("[id='{!$Component.dataform.pageblock.section.wo}']").focus();
    });
</script>

But the code is not working. the focus will always be on the first field. I tried to do an alert after the set focus statement, and it looks like the focus is set to the second field when it alerts, but for some reason, it is set back to the first field after the alert. Have any ideas?
Best Answer chosen by CharlesDou