By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,982 Members | 1,955 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,982 IT Pros & Developers. It's quick & easy.

Form focus

P: n/a
GTi
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>

Jan 19 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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)
Jan 19 '06 #2

P: n/a
GTi
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.

Jan 19 '06 #3

P: n/a
GTi wrote on 19 jan 2006 in comp.lang.javascript:
To make a long story short:
I don't have full access to the whole document from my ASP.NET code.
I would not find that acceptable ;-)
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")
You need an extra ) here.

Now it works
{
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.


try:

<form>
<input type='submit'>
</form>

<form>
<input>
</form>
<script type="text/JavaScript">

for (var f =0; f < document.forms.length;f++) {
var field = document.forms[f];
for (var i = 0; i < field.length; i++) {
e = field.elements[i]
if ((e.type == "text")
|| (e.type == "textarea")
|| (e.type.toString().charAt(0) == "s")) {
e.focus();
break;

}
}
}

</script>

The == 's' does not seem to work


--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Jan 19 '06 #4

P: n/a
GTi
Evertjan. wrote:
GTi wrote on 19 jan 2006 in comp.lang.javascript:
To make a long story short:
I don't have full access to the whole document from my ASP.NET code.
I would not find that acceptable ;-)
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")


You need an extra ) here.

I Know.... copy paste problem.

Now it works
{
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.

try:

<form>
<input type='submit'>
</form>

<form>
<input>
</form>
<script type="text/JavaScript">

for (var f =0; f < document.forms.length;f++) {
var field = document.forms[f];
for (var i = 0; i < field.length; i++) {
e = field.elements[i]
if ((e.type == "text")
|| (e.type == "textarea")
|| (e.type.toString().charAt(0) == "s")) {
e.focus();
break;

}
}
}

</script>

The == 's' does not seem to work

I know ;)


--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)


Anyway - I don't think this is a big problem for now since most users
use the mouse on combo boxes.

Jan 19 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.