+ Start a Discussion
Rajat Mahajan 21Rajat Mahajan 21 

Need to go to top of page after an error is thrown in Visualforce page

Hi All

One of our VF pages length is long and the messages get shown at the top of the page.
However, when messages are shown , the user needs to navigate to top to check error.

Are there any workarounds to send the user to the top of the page on any click ?

Many thanks in advance,
Rajat
Vivek DeshmaneVivek Deshmane
Hi Rajat,
Could you please try below code and change it according to your need and let me know if it works.
 
<apex:page>
<apex:pageMessage id="msgId"/>
<apex:commandbutton value="save" and oncomplete="setFocus()"/>
<script>
function setFocus(){
var msgComp=document.getElementById(
    "{!$Component.theBlock.theSection.theSectionItem.theText}")
	if(msgComp.innerText!=null && msgComp.innerText!='undefined'&&msgComp.innerText.length>0)
	{
		msgComp.focus()
	}
}	
	
</script>
</apex:page>

Best Regards,
-Vivek