469,356 Members | 1,918 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,356 developers. It's quick & easy.

Problem with Form Focus


I focus on controls in a web form like this:

<script language="javascript">
var focusControl = null;
if (document.getElementById)
focusControl = document.getElementById('txtArea1');
else if (document.all)
focusControl = document.all['txtArea1'];
if (focusControl && focusControl.focus)
focusControl.focus();
</script>

This works on all of my textboxes and textareas except for 2. For them,
if I put a "alert(focusControl.id);" after the "focusControl.focus();",
then after the user clicks Enter the controlg gets focus. But if I
don't put that alert in there then the address bar (?!) gets focus.

How can I make this perfect and foolproof? I've looked at my HTML
thousands of times and the textareas are like all the others that work
without an alert box.

Thanks.

Jul 23 '05 #1
3 2857
br************@hotmail.com wrote:
I focus on controls in a web form like this:

<script language="javascript">
The language attribute is depreciated, type is required:

<script type="text/javascript">

But that has nothing to do with your issue :-(
var focusControl = null;
if (document.getElementById)
focusControl = document.getElementById('txtArea1');
else if (document.all)
focusControl = document.all['txtArea1'];
if (focusControl && focusControl.focus)
focusControl.focus();
</script>

This works on all of my textboxes and textareas except for 2. For them,
if I put a "alert(focusControl.id);" after the "focusControl.focus();",
then after the user clicks Enter the controlg gets focus. But if I
don't put that alert in there then the address bar (?!) gets focus.

How can I make this perfect and foolproof? I've looked at my HTML
thousands of times and the textareas are like all the others that work
without an alert box.


Are you running this from the page as it loads?

It may well be that the browser has not finished creating 'txtArea1'
when the script is called, so focus goes to the address bar. The
alert gives the browser time to finish the page and do the focus.

Try running the script using body onload and see if that helps.
--
Rob
Jul 23 '05 #2
br************@hotmail.com wrote:
I focus on controls in a web form like this:

<script language="javascript">
<script type="text/javascript">

see numerous explanations in this group.
var focusControl = null;
if (document.getElementById)
focusControl = document.getElementById('txtArea1');
else if (document.all)
focusControl = document.all['txtArea1'];
If you use document.all, use it as a method (parens), not as a collection
(brackets).
if (focusControl && focusControl.focus)
focusControl.focus();
Regarding feature detection this is a valid approach in most cases, but
could result in a script error anyway; it only tests whether that host
object property exists, not if it is a method (property of type "function")
as required by the Call Operator `(...)'. Therefore, I have developed
an efficient and easy-to-use method for testing methods some time ago.
Feel free to use it if you take heed of the License:

/**
* @author
* (C) 2003, 2004 Thomas Lahn &lt;ty******@PointedEars.de&gt;
* Distributed under the GNU GPL v2 and above.
* @optional Object|string o
* Object to be determined an method, i.e. a
* <code>Function</code> object assigned as property of
* another object. May also be a string to be evaluated
* and so is applicable to unknown properties.
* @return type boolean
* <code>true</code> if <code>o</code> is a method,
* <code>false</code> otherwise.
* @see #isMethodType()
*/
function isMethod(m)
{
var t;
(m = eval(m)) && (t = typeof m);
return (t == "function" || t == "object");
}

You would use it here like this:

if (focusControl && isMethod(focusControl.focus))
{
focusControl.focus();
}
</script>

This works on all of my textboxes and textareas except for 2. For them,
if I put a "alert(focusControl.id);" after the "focusControl.focus();",
then after the user clicks Enter the controlg gets focus. But if I
don't put that alert in there then the address bar (?!) gets focus.
You should post the source code of those two textboxes/textareas if the
below does not help:
How can I make this perfect and foolproof? [...]
Whenever forms are involved, try to avoid getElementById(), document.all()
aso. and use DOM Level 0 downwards compatible referencing:

focusControl = document.forms[...].elements[...];

This is downwards compatible with names and works in current UAs with IDs
as well, see the W3C DOM Level 2 HTML Specification.
I've looked at my HTML thousands of times and the textareas are like all
the others that work without an alert box.


Can't be. However, you should not continue that practice at all: Focusing
a control with client-side scripting almost always prevents the user from
scrolling the respective document with non-pointing devices, such as a
keyboard.
PointedEars
Jul 23 '05 #3
Thomas 'PointedEars' Lahn wrote:
br************@hotmail.com wrote:

<snip>
focusControl = document.all['txtArea1'];


If you use document.all, use it as a method (parens),
not as a collection (brackets).

<snip>

Quite the reverse. There are browsers implementing the - document.all -
collection exclusively as an object (rather than as a function object)
so treating the - all - collection as a function will error in those
environments, while all the browsers where the - all - collection can be
called also allow it to be treated as an object, and property accessors
used. And referencing a member of the - all - collection with a property
accessor should be faster as there are inevitable overheads in a
function call.

Generally no collection should be treated as a function, despite the
fact that doing so is usually effective in IE (and directly proposed in
much Microsoft documentation [1]). It is a bad habit to ever get into
and, if done habitually, will become needless barrier to cross-browser
scripting.

Richard.

[1] Microsoft often seem to take the stance that they have won the
browser war, so it is not particularly surprising to find their
documentation proposing practices that would act against cross-browser
coding.
Jul 23 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Iver Erling Årva | last post: by
3 posts views Thread by Marc Llenas | last post: by
1 post views Thread by cider123 | last post: by
1 post views Thread by avnrao | last post: by
18 posts views Thread by Derek Martin | last post: by
reply views Thread by Bruin | last post: by
1 post views Thread by clickon | last post: by
21 posts views Thread by cmd | last post: by
3 posts views Thread by jan.loucka | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.