Evertjan. wrote:
GTi wrote on 19 jan 2006 in comp.lang.javascript:
Is it possible to have a generic script that set the input focus on the
first valid element in a document (not hidden or disabled) ?
This script is at the end of a document, but don't work.
<script type="text/javascript">
document.forms[0].elements[0].focus();
</script>
Perhaps the DOM is notyet finished being build.
Use body onload='' or a setTimer().
(not hidden or disabled)
It is your document, so why make a long script for this?
Try:
<body onload='document.getElementById("firstOne").focus( )'>
....
<input id='firstOne'>
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Evertjan,
To make a long story short:
I don't have full access to the whole document from my ASP.NET code.
Explanation:
I use plug in technology and my html code is placed in a placeholder on
a master plug in page. The main page don't know about the form element
names in the document. And the plug in page don't have access to the
main body.
However I did find this script:
if(document.forms.length > 0)
{
var field = document.forms[0];
for (i = 0; i < field.length; i++)
{
if ((field.elements[i].type == "text")
|| (field.elements[i].type == "textarea")
|| (field.elements[i].type.toString().charAt(0) == "s")
{
document.forms[0].elements[i].focus();
break;
}
}
}
But it don't put the focus on any select items.
I think field.elements[i].type.toString().charAt(0) == "s"
is trying to solve it, but it dont.