How to Set Focus to Web Form Controls By Using Client-Side Script

Platform: ASP.NET| Category: Basic

<script language='javascript'>
function SetFocus()
{
	// W3C approved DOM code that will work in all modern browsers	
	if (document.getElementById)
          		document.getElementById(’txt2’).focus();
     	else
     	// To support older versions of IE:
         	if (document.all)
            		document.all('txt2').focus();
    	return false;
}
</script>

<body MS_POSITIONING='GridLayout' onload='SetFocus()'>
<form id='Form1' method='post' runat='server'>
	Enter 1:
	<asp:TextBox ID='txt1' Runat='server' Width='50' />
	<br>
	Enter 2:
	<asp:TextBox ID='txt2' Runat='server' Width='50' />
	<br>
	<asp:Button id='Button1' runat='server' Text='Button1'></asp:Button>
</form>
</body>

Refer
Sample IE Code

Share with

Related FAQs

Couldn't find the FAQs you're looking for?

Please submit your question and answer.