Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to make Text Box automatically selected on page_load

Status
Not open for further replies.

markknowsley

Programmer
Aug 30, 2005
152
GB
If you look at or you will notice that the text boxes which take input for the search string or the username automatically have a flashing cursor in them ready to receive input.

How do I do this to an asp text box? I've tried 'TabIndex="1"' but this didn't have the desired effect.
 
It's a little method in JavaScript that is called focus(). You can basically call it on every element, but it is mostly used in form elements. In google (and many other websites that do the same) there is an onload event in the document body which gives focus to the desired element. I suggest you use getElementById() to grab the element you want to give it focus, given that the element has an id (which if you use labels, it probably does). If you need detailed information, this would better be answered in forum216, since we're hanging out of the scope of html here.
 
You need to call a javascript function in body onLoad...
Code:
<HEAD>
<script type="text/javascript">
function setfocus()
{
document.forms[0].txt1.focus();
}
</script>
</HEAD>
<BODY onload=setfocus()>
.....txt1 being your textbox where you want to focus initially.

Sharing the best from my side...

--Prashant--
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top