473,396 Members | 1,726 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

How to focus the cursor in an input field when accessing a site

How do I focus the cursor in the input field 'numberField' when accessing
this jsp-file (or html-file) ?
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
<title>Untitled</title>
</head>
<body>

<form action="" method="post">
<input type="text" name="numberField" size="30" >
<p><input type="submit" name="buttonAddNumber" value="Add a number">
<input type="submit" name="buttonRemoveNumber" value="Remove a number">
</form>

</body></html>
Regard
Rune
Jul 23 '05 #1
5 2094
Rune Runnestø wrote:
How do I focus the cursor in the input field 'numberField' when accessing
this jsp-file (or html-file) ?
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
<title>Untitled</title>
</head>
<body>

<form action="" method="post">
<input type="text" name="numberField" size="30" >
<p><input type="submit" name="buttonAddNumber" value="Add a number">
<input type="submit" name="buttonRemoveNumber" value="Remove a number">
</form>

</body></html>


<body onload="
if(document.forms[0].elements['numberField'].focus)
document.forms[0].elements['numberField'].focus();
">

If you give your form a name, then you can substitute that for the
'0' in forms[0] and your script will not be dependent on the field
being in the first form.

e.g. if your form is called "formA", then:

<body onload="
if(document.forms['formA'].elements['numberField'].focus)
document.forms['formA'].elements['numberField'].focus();
">

....

<form action="" method="post" name="formA">

....

--
Rob
Jul 23 '05 #2


Rune Runnestø wrote:

<body>

<form action="" method="post">
<input type="text" name="numberField" size="30" >


<script type="text/javascript">
document.forms[0].elements.numberField.focus();
</script>


--

Martin Honnen
http://JavaScript.FAQTs.com/
Jul 23 '05 #3

Code
-------------------

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
<title>Untitled</title>
<SCRIPT TYPE=\"TEXT/JAVASCRIPT\">
FUNCTION FNSETFOCUS(ELEMID)
{ DOCUMENT.GETELEMENTBYID(ELEMID).FOCUS();
}
</SCRIPT
</head>
<body *onload="fnSetFocus('numField');"*>

<form action="" method="post">
<input type="text" name="numberField"* id="numField"* size="30" >
<p><input type="submit" name="buttonAddNumber" value="Add a number">
<input type="submit" name="buttonRemoveNumber" value="Remove a number">
</form>

</body></html>
-------------------

--
stymi
-----------------------------------------------------------------------
stymie's Profile: http://www.highdots.com/forums/member.php?userid=3
View this thread: http://www.highdots.com/forums/showthread.php?t=26796

Jul 23 '05 #4
> Code:
--------------------

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
<title>Untitled</title>
<SCRIPT TYPE=\"TEXT/JAVASCRIPT\">
FUNCTION FNSETFOCUS(ELEMID)
{ DOCUMENT.GETELEMENTBYID(ELEMID).FOCUS();
}
</SCRIPT>
It doesn't work in my browser with UPPERCASE. But this works fine:

<script language="JavaScript" type="text/javascript">
function fnSetFocus(elemid){
document.getElementById(elemid).focus();
}
</script>
Maybe Javascript is case-sensitive.
Thanks anyway !

Regards
Rune
</head>
<body *onload="fnSetFocus('numField');"*>

<form action="" method="post">
<input type="text" name="numberField"* id="numField"* size="30" >
<p><input type="submit" name="buttonAddNumber" value="Add a number">
<input type="submit" name="buttonRemoveNumber" value="Remove a number">
</form>

</body></html>
--------------------

Jul 23 '05 #5
Rune Runnestø wrote:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
<title>Untitled</title>
<SCRIPT TYPE=\"TEXT/JAVASCRIPT\">
FUNCTION FNSETFOCUS(ELEMID)
{ DOCUMENT.GETELEMENTBYID(ELEMID).FOCUS();
}
</SCRIPT>
It doesn't work in my browser with UPPERCASE.


The code above is really FUBAR.
But this works fine:

<script language="JavaScript" type="text/javascript">
You can safely omit the deprecated language="JavaScript".
function fnSetFocus(elemid){
document.getElementById(elemid).focus();
No need to use document.getElementById(), creating a dependency on IDs and
the W3C DOM. Use

document.forms[0].elements[elemid].focus();

instead. To support more than one form per document, redesign the method:

function fnSetFocus(f, el)
{
if (f && el
&& (f = document.forms)
&& f.elements
&& (el = f.elements[el])
&& el.focus);
{
el.focus();
}
}

This will works with names as well.
}
</script>

Maybe Javascript is case-sensitive.
It is, and so spelled "JavaScript" :)
However, this feature applies for all ECMAScript implementations.
Thanks anyway !
Thanks for reading before posting, including ...
[Top post]


.... the newsgroup's FAQ.
PointedEars
--
When the power of love overcomes the love
of power, the world will know peace.
-- Jimi Hendrix
Jul 23 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: ra | last post by:
Hi I can't figure this out - I have a routine that is triggered after a text field is changed and if an error it displays an alert message and should put focus back to the field in error but...
11
by: JCO | last post by:
I have a text box, on my form that I cant get the focus on. I'm using it as shown: <head> <body onload=document.getPassword.txtPasswordName.focus()> </head> Where getPassword is the form &...
4
by: Scott Navarre | last post by:
Hi, I have Red Hat 8.0 and have the default Mozilla browser that comes with it. I am programming in javascript and have come across something problematic. Given the following code: <HTML>...
2
by: dsnyder | last post by:
This HTML has a bit of Javascript at the end that puts the initial focus on the userID field. It works great on Windows2000 running IE6, but the initial focus never goes to the userID field on...
6
by: Csaba | last post by:
I'd like to have the cursor in the login form's username input text field when users load the login page. It works fine with the following implementation: <body...
3
by: Dai Ba Wong | last post by:
Hi: Currently I am having a problem with my webpage. My page consist of two frames, one consist of input text field and the other contains link for different pop-up windows. The problem...
7
by: ScooterMX | last post by:
I've seen where login pages (and other form-based pages) place the cursor in the first field of a form after the page loads. I'm familiar with the tabIndex, but that only works once you hit the...
2
by: Rey | last post by:
Howdy all. Using visual web developer (VB) on xp pro box. My problem with with a web form that on accessing the calendar control causes a postback that moves the cursor back to the txtFirstName...
11
by: sheldonlg | last post by:
I have a problem with not seeing the current focus while in a particular screen in a my app. The focus does not show for a type=text or textarea. I have tried to reproduce this problem in a test...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.