What I need is in the Visualforce Page, I have created a command button doing the validation of all input data before saving them into the Salesforce objet. This validation code located at the Apex class module, during the validation, if there's any invalid data typed by the user, I have to display the corresponding Error message then set the focus to the corresponding field in the Visualforce Page. For example, for the invalid input Zip code then the focus should set to the Zip code field, for an invalid Phone number then the focus should set to the Phone number field... So I think the set focus instruction should be written in the Apex Class and not in the Visualforce Page.
Please note that sandeep's solution above could override the standard "onload" functions that SFDC puts onto the page... thus preventing standard SFDC functionality from working (like date pop-ups and other things that they use javascript for).
A safer way to do this is to append your "setFocus" function to the existing window onload functions (if any).
<script type="text/javascript">
function addLoadEvent(func) {
var oldonload = window.onload;
if (typeof window.onload != 'function') {
window.onload = func;
} else {
window.onload = function() {
oldonload();
func();
}
}
}
function setFocus() {
document.getElementById('{!$Component.___}').focus();
}
addLoadEvent(setFocus);
</script>
Where "$Component.___" is the necessary id reference to the field to which you want to set focus.
Need some help here too. I have this UI which has multiple input boxes on the page and I want foucs on the 1st field. Something like this: <div class="slds-form-element__control"> <apex:inputText id="firstNameValue" required="true" value="{!firstName}" styleClass="slds-input text-input"/> </div> Could someone help me on how do I focus this one? I tried what is recommended above but I am probably missing something. Cant get it done? Thanks a lot
Add this javascript code to the bottom your page (before the page closing tag):
Cheers
Thanks Sam for your quick response!
What I need is in the Visualforce Page, I have created a command button doing the validation of all input data before saving them into the Salesforce objet. This validation code located at the Apex class module, during the validation, if there's any invalid data typed by the user, I have to display the corresponding Error message then set the focus to the corresponding field in the Visualforce Page. For example, for the invalid input Zip code then the focus should set to the Zip code field, for an invalid Phone number then the focus should set to the Phone number field... So I think the set focus instruction should be written in the Apex Class and not in the Visualforce Page.
Best regards!
This might not be the best solution but it will work:
<!-- To set focus on Top->
<input id="hiddenElementId" type="hidden" />
<script type="text/javascript">
window.onload = setFocus
function setFocus() {
document.getElementById("hiddenElementId").focus();
}
</script>
Thanks, its work
A safer way to do this is to append your "setFocus" function to the existing window onload functions (if any).
Where "$Component.___" is the necessary id reference to the field to which you want to set focus.
<div class="slds-form-element__control">
<apex:inputText id="firstNameValue" required="true" value="{!firstName}" styleClass="slds-input text-input"/>
</div>
Could someone help me on how do I focus this one? I tried what is recommended above but I am probably missing something. Cant get it done? Thanks a lot