Connecting Tech Pros Worldwide Forums | Help | Site Map

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

Rune Runnestø
Guest
 
Posts: n/a
#1: Jul 23 '05
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



RobG
Guest
 
Posts: n/a
#2: Jul 23 '05

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


Rune Runnestø wrote:[color=blue]
> 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>[/color]

<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
Martin Honnen
Guest
 
Posts: n/a
#3: Jul 23 '05

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




Rune Runnestø wrote:

[color=blue]
> <body>
>
> <form action="" method="post">
> <input type="text" name="numberField" size="30" >[/color]

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




--

Martin Honnen
http://JavaScript.FAQTs.com/
stymie
Guest
 
Posts: n/a
#4: Jul 23 '05

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



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

Rune Runnestø
Guest
 
Posts: n/a
#5: Jul 23 '05

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


> Code:[color=blue]
> --------------------
>
> <!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>[/color]

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
[color=blue]
> </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>
>
>
> --------------------[/color]


Thomas 'PointedEars' Lahn
Guest
 
Posts: n/a
#6: Jul 23 '05

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


Rune Runnestø wrote:
[color=blue][color=green]
>> <!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>[/color]
>
> It doesn't work in my browser with UPPERCASE.[/color]

The code above is really FUBAR.
[color=blue]
> But this works fine:
>
> <script language="JavaScript" type="text/javascript">[/color]

You can safely omit the deprecated language="JavaScript".
[color=blue]
> function fnSetFocus(elemid){
> document.getElementById(elemid).focus();[/color]

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.
[color=blue]
> }
> </script>
>
> Maybe Javascript is case-sensitive.[/color]

It is, and so spelled "JavaScript" :)
However, this feature applies for all ECMAScript implementations.
[color=blue]
> Thanks anyway ![/color]

Thanks for reading before posting, including ...
[color=blue]
> [Top post][/color]

.... the newsgroup's FAQ.


PointedEars
--
When the power of love overcomes the love
of power, the world will know peace.
-- Jimi Hendrix
Closed Thread


Similar JavaScript / Ajax / DHTML bytes