Print this page

When loading a Visualforce Page with Picklist and Text Field, focus is always on Text

Knowledge Article Number 000149029
Description When a Visualforce page containing text and picklist inputfields is rendered, the cursor or focus is always on the text field, even if the picklist is above the text field on the page. How do you focus on the picklist?
Resolution Try using a javascript function with focus() method onload:
Note: Salesforce does not support custom Javascript code. Please make sure to test this first and use only if it works. 
<apex:page standardController="Opportunity" >
	<script>
		function setFocusOnLoad() {
			document.getElementById('{!$Component.pg.pg1.pg2.val1}').focus();
		}
		window.onload=setFocusOnLoad;
	</script>
	<apex:form >
		<apex:pageblock id="pg">
			<apex:pageBlockSection title="Opportunity Information Input" columns="1" id="pg1">
				<apex:pageBlockSectionItem id="pg2" >
					<apex:inputfield id="val1" value="{!Opportunity.StageName}"/>
					<apex:inputfield value="{!Opportunity.StageName}"/>
				</apex:pageBlockSectionitem>
				<apex:pageBlockSectionItem >
					<apex:inputfield value="{!Opportunity.Name}"/>
					<apex:inputfield value="{!Opportunity.Name}"/>
				</apex:pageBlockSectionitem>
				<apex:pageBlockSectionItem >
					<apex:inputfield value="{!Opportunity.Name}"/>
					<apex:inputfield value="{!Opportunity.Name}"/>
				</apex:pageBlockSectionitem>
			</apex:pageBlockSection>
		</apex:pageblock>
	</apex:form> 
</apex:page>

 




promote demote