473,396 Members | 1,898 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.

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 8714
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: leiko | last post by:
I've written the following function who works great in IE function OnlyCharacter(tekst) { if (tekst.length == 1) { if (tekst >= "A" && tekst <="Z") {return true} } window.alert ("Only...
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...
7
by: JW | last post by:
hello everybody I've got the following problem. I'm writing a form and I'm using javascript to validate fields (i.e. numeric, not blank). When my scripts detects an error I want it to put the...
2
by: Peter Wright | last post by:
Hi all. Hopefully this should demonstrate the problem I'm having: http://flooble.net/~pete/focus-problem-demo/ (I'm testing it in Mozilla only, but I'm not sure if it's actually a...
5
by: Rune Runnestř | last post by:
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>...
3
by: Stefan Mueller | last post by:
I've an input box <input type = "text" name = "MyInput" value = ""> and a selection <select name = "MySelection" size = "1"> <option value = "1">Entry 1 <option value = "2">Entry 2 </select>...
2
by: yawnmoth | last post by:
Say I have two input elements and that I wanted to make it so that when the first ones input was what it should be, the focus would automatically be shifted to the next input element. ie....
1
by: firenet | last post by:
21 function js_reply_msg(node,g_id,u_id,par_id) 22 { 23 node.innerHTML="<FORM><TEXTAREA name=\"msg_con\"><\/TEXTAREA><br><INPUT type=\"submit\" value=\"reply\"><\/FORM>" 24 ...
1
by: Will_uk | last post by:
I am trying to return focus to a form input field. If i use <form>.<field>.focus() the focus returns to the field but if the field already contains a value it selects it (blacks it out). Is it...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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?
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.