IT Community - Software Programming, Web Development and Technical Support

How do I set the focus to the first form field?

This is a discussion on How do I set the focus to the first form field? within the HTML, CSS and Javascript Coding Techniques forums, part of the Web Development category; How do I set the focus to the first form field?...


Go Back   IT Community - Software Programming, Web Development and Technical Support > Web Development > HTML, CSS and Javascript Coding Techniques

Register FAQ Members List Calendar Mark Forums Read
  1 links from elsewhere to this Post. Click to view. #1 (permalink)  
Old 07-27-2007, 03:08 AM
oxygen oxygen is offline
D-Web Architect
 
Join Date: Jun 2007
Posts: 633
oxygen is on a distinguished road
Question How do I set the focus to the first form field?

How do I set the focus to the first form field?
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 07-27-2007, 03:25 AM
vijayanand vijayanand is offline
D-Web Analyst
 
Join Date: Feb 2007
Posts: 293
vijayanand is on a distinguished road
Default Re: How do I set the focus to the first form field?

u can set focus to the first form field by using the following javascript function after the first form element "form_field_name"

<script>
document.getElementById('form_field_name').focus() ;
</script>

where "form_field_name" is the first form field ID
__________________

J.Vijayanand
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 07-27-2007, 03:34 AM
kingmaker kingmaker is offline
D-Web Genius
 
Join Date: Jun 2007
Posts: 882
kingmaker is on a distinguished road
Send a message via Yahoo to kingmaker
Default Re: How do I set the focus to the first form field?

You cannot do this with HTML. However, you can include a script after the form that sets the focus to the appropriate field, like this:

<form id="myform" name="myform" action=...>
<input type="text" id="myinput" name="myinput" ...>
</form>

<script type="text/JavaScript">
document.myform.myinput.focus();
</script>

A similar approach uses <body onload=...> to set the focus, but some browsers seem to process the ONLOAD event before the entire document (i.e., the part with the form) has been loaded
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 08-08-2007, 01:33 AM
varghese varghese is offline
D-Web Programmer
 
Join Date: Feb 2007
Posts: 93
varghese is on a distinguished road
Default Re: How do I set the focus to the first form field?

just you set index of the form field.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 08-08-2007, 03:02 AM
krishnakumar krishnakumar is offline
D-Web Analyst
 
Join Date: May 2007
Posts: 206
krishnakumar is on a distinguished road
Wink Re: How do I set the focus to the first form field?

Hi,
We can set the focus to the first form field by using this code...
U can try this code where u need... hope u get better result...

Here Codes,
<HTML>
<HEAD>

<SCRIPT LANGUAGE="JavaScript">

function toForm()
{
document.form.field1.focus();

}
</script>

</HEAD>

<BODY onLoad="toForm()">

<FORM NAME="form" METHOD="POST" ACTION="#">
<SELECT NAME="field1" SIZE="1">
<OPTION VALUE="one">Field 1
<OPTION VALUE="two">also field1
</SELECT>
<BR>
<INPUT TYPE="TEXT" NAME="field2" SIZE="10"> Field 2<BR>
<INPUT TYPE="TEXT" NAME="field3" SIZE="10"> Field 3<BR>
<BR>
<INPUT TYPE="SUBMIT" NAME="submit" VALUE="Submit">
<INPUT TYPE="RESET" onclick="toForm();">
</FORM>

<p><center>
<font face="arial, helvetica" size"-2">Free JavaScripts provided<br>
by <a href="http://javascriptsource.com">The JavaScript Source</a></font>
</center><p>
</BODY>
</HTML>
__________________
Krishnakumar.S
Beware of Everything -that is un true; stick to the Truth shall succeed slowly but steadily
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

LinkBacks (?)
LinkBack to this Thread: http://www.discussweb.com/html-css-javascript-coding-techniques/2947-how-do-i-set-focus-first-form-field.html
Posted By For Type Date
DiscussWeb IT Community - Fusing This thread Refback 08-08-2007 04:10 AM

Similar Threads
Thread Thread Starter Forum Replies Last Post
Date/time stamp a form field as the form is submitted itbarota HTML, CSS and Javascript Coding Techniques 1 02-06-2008 10:16 PM
Date/Time stamp a form field as the form is submitted itbarota HTML, CSS and Javascript Coding Techniques 2 01-17-2008 08:47 PM
How can I clear the form field values after the user has submitted the form for clien itbarota HTML, CSS and Javascript Coding Techniques 1 11-01-2007 11:39 PM
How can I set the value of a fileupload form field? kingmaker HTML, CSS and Javascript Coding Techniques 0 10-31-2007 03:08 AM
How can I uncheck a radio form field? Pvinothkumar HTML, CSS and Javascript Coding Techniques 1 10-17-2007 11:25 PM


All times are GMT -7. The time now is 11:07 AM.


Copyright ©2004 - 2007, DiscussWeb. All Rights Reserved.

SEO by vBSEO 3.0.0