Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Setting focus to the INPUT element throws error message

Status
Not open for further replies.

gizmicek

Programmer
Jun 25, 2003
107
CZ
Hi, I have the following code on my pages which is supposed to set focus to the login form when the user is not logged in:
Code:
onload="document.forms[0].elements[0].focus();"

When the user is not logged in, this is the only FORM which appears on the pages so accessing it through the forms[0] should be OK. The thing is that it works fine only sometimes. When I start the IE and it goes to my pages by default (I have set it as start page), I get an error which tells me that it's not possible to set focus to the element because it is either hidden or disabled. But the element is enabled and visible. Should I access the form and the element through names? If yes, what is the correct syntax for accessing the FORM named "login"? I presume that it is something like
Code:
forms['login'].elements['loginnameinput']
but I'm not sure.

Any help will be appreciated.
 
This is a working example, maybe it will give you
an idea(be sure your textbox is inside form tags):

Code:
<html>
<head>
<title>testbed</title>
<script language=&quot;JavaScript&quot; type=&quot;text/javascript&quot;>
function setFocus() {
document.forms[0].elements[0].focus();
}
</script>
</head>
<body onLoad=&quot;setFocus();&quot;>
<form>
<input type=&quot;text&quot; name=&quot;field1&quot; />
<input type=&quot;text&quot; name=&quot;field2&quot; />
</form>
</body>
</html>

2b||!2b
 
Thanx for suggestion. Your code works the same way as my previous so there will be maybe problem somewhere else - it seems to me that this problem occurs only when I access the page for the first time. If I log out, the focus is correctly set to the login name INPUT. I have one SWF object on my pages which maybe delayes the loading of BODY or something like this and maybe the focus is set to the control before it is loaded. But it sounds a little confusing for me. I will try to investigate this much further and will let you know about results.

PS: I wrote the thing about the SWF element because I'm turning off the page caching using HTTP headers on my pages and the only thing which is still cached is the ShockWave Flash animation.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top