input focus

L

linuxnooby

This is a simple question but it is doing my head in

all i want is a form text field to be focused when the page loads.
Works in IE6 but not in Firefox 1.6

<body >
<form action="testfocus.htm" method="post" name="test" id="test" >
<input type="text" name="name" id="name"
onLoad="this.forms[0].elements[0].focus()">
<input type="submit" name="submit">
</form>
</body>


I tried variations like

onLoad="this.forms["test"].elements["name"].focus()"
onLoad="this.getByElementId["name"].focus()"
onLoad="this.test.name.focus()"

these all work in IE but not Firefox!!! I need to have the onload event
in the form tag because the body tag is part of an include that is
common to other pages.

help!!!!!!!!!!!!!!!!!!!!!!!!!!
dave
 
R

RobG

This is a simple question but it is doing my head in

all i want is a form text field to be focused when the page loads.
Works in IE6 but not in Firefox 1.6

<body >
<form action="testfocus.htm" method="post" name="test" id="test" >
<input type="text" name="name" id="name"
onLoad="this.forms[0].elements[0].focus()">

The HTML 4 specification defines an onload attribute for body and
frameset elements only. Some browsers support it on a wider selection
of elements, but that can't be relied upon.

Also, if you want a form control to refer to itself, just use 'this':

<input ... onload="this.focus()">


Of course that will still not work in browsers that don't support the
onload attribute for input elements.

You are much better to define a body onload attribute:

<body onload="focusOn(document.forms['test'].elements['name']);">

And the focusOn function could be:

function focusOn(el)
{
if (el.focus) el.focus();
}

<input type="submit" name="submit">
</form>
</body>


I tried variations like

onLoad="this.forms["test"].elements["name"].focus()"
onLoad="this.getByElementId["name"].focus()"
onLoad="this.test.name.focus()"

these all work in IE but not Firefox!!! I need to have the onload event
in the form tag because the body tag is part of an include that is
common to other pages.

help!!!!!!!!!!!!!!!!!!!!!!!!!!
dave
 
K

kay

cant u just put somewhere inside the body tag

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

? i think it will work...?
 
R

RobG

kay said:
cant u just put somewhere inside the body tag

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

? i think it will work...?

aamof u can bta itfa u might find it aint the best ymmv

ttfn
 
T

Thomas 'PointedEars' Lahn

kay said:
cant u just put somewhere inside the body tag

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

? i think it will work...?

It does not. First, "somewhere" is not sufficient: the `script' element
at least must be placed after the form element, and even then there is no
guarantee that it is going to work. Second, it works only where an
HTMLDocument object is in the scope chain; so far, at least that does not
apply to the Gecko DOM, the Opera DOM and the KHTML DOM.

Please do not post suggestions if you do not have at least a minimum clue
of what you are talking about, and if you have not tested your unfounded
assumptions once at least against two actual implementations; displaying
your fantasies here is not going to help anybody.

Last but not least: Would you please follow at least the basic rules of the
natural language you use? "cant", "u" and "i" are not words of the English
language, it should be at least "can't" (short for "cannot"), "you", and
"I". Also English is not Spanish, for example: interrogative sentences are
indicated by only a trailing question mark (and a question like "I think it
will work?" does not make much sense anyway, because nobody but you can
know what you think). You are not using a Web forum here, short of a chat
platform, where your current language style might be an appropriate one.
That said, it is well-known here that I am a speaker of English as foreign
language myself, and even I resent your style. That definitely should make
you reconsider.

See also

<http://jibbering.com/faq/faq_notes/pots1.html>
<http://safalra.com/special/googlegroupsreply/>

regarding your inappropriate posting style.


HTH

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

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,077
Latest member
SangMoor21

Latest Threads

Top