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

R

Rune Runnestø

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
 
R

RobG

Rune said:
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">

....
 
M

Martin Honnen

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>
 
R

Rune Runnestø

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
 
T

Thomas 'PointedEars' Lahn

Rune said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top