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 Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

form focus first field

Status
Not open for further replies.

overflo

Technical User
Feb 10, 2003
70
AU
I need to force the focus onto the first field of a form. I know how to do this in javascript for a html form but what if the forms are generated from functions (It's a fully dynamic site)?
Any help would be greatly appreciated
 
You would do it the same. PHP does its work dynamically on the server and returns the same html code to the client as if the site were completely static.
 
The code below should give you what you need. However, my Javascript ain't the best in the world, so you may want to alter that to be more specific to your forms.

Ensure that the Javascript comes after the form in the HTML, otherwise it won't work.

Code:
<html>
<head></head>
<body>
<form name="myform">
<input type="text" name="myinput1" />
<input type="text" name="myinput2" />
<input type="text" name="myinput3" />
<input type="text" name="myinput4" />
<input type="text" name="myinput5" />
</form>

<script type="text/javascript">
//<![CDATA[
document.forms[document.forms.length-1].myinput1.focus();
//]]>
</script>
</body>
</html>


Ahdkaw
 
Just a quick note to the code posted by ahdkaw - the first field of the LAST form on the page will be selected. Not a big deal normally - but depending on the source code order on the client browser it could cause unexpected results (if, for instance, a search form was after the main content in source order).

Cheers,
Jeff

[tt]Jeff's Page @ Code Couch
[/tt]

What is Javascript? FAQ216-6094
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top