471,337 Members | 985 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

input focus

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

Feb 28 '06 #1
5 8613
li********@yahoo.com.au wrote:
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


--
Rob
Feb 28 '06 #2
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...?

Mar 1 '06 #3
kay wrote:
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
--
Rob
Mar 1 '06 #4
kay wrote:
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
Mar 1 '06 #5
Thanks Rob

Mar 1 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by leiko | last post: by
6 posts views Thread by Csaba | last post: by
3 posts views Thread by Stefan Mueller | last post: by
2 posts views Thread by yawnmoth | last post: by
1 post views Thread by Will_uk | last post: by

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.