Form focus

G

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>
 
E

Evertjan.

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'>
 
G

GTi

Evertjan. said:
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,

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.type == "text")
|| (field.elements.type == "textarea")
|| (field.elements.type.toString().charAt(0) == "s")
{
document.forms[0].elements.focus();
break;
}
}
}


But it don't put the focus on any select items.
I think field.elements.type.toString().charAt(0) == "s"
is trying to solve it, but it dont.
 
E

Evertjan.

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.type == "text")
|| (field.elements.type == "textarea")
|| (field.elements.type.toString().charAt(0) == "s")


You need an extra ) here.

Now it works
{
document.forms[0].elements.focus();
break;
}
}
}


But it don't put the focus on any select items.
I think field.elements.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
if ((e.type == "text")
|| (e.type == "textarea")
|| (e.type.toString().charAt(0) == "s")) {
e.focus();
break;

}
}
}

</script>

The == 's' does not seem to work
 
G

GTi

Evertjan. said:
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.type == "text")
|| (field.elements.type == "textarea")
|| (field.elements.type.toString().charAt(0) == "s")


You need an extra ) here.

I Know.... copy paste problem.
Now it works
{
document.forms[0].elements.focus();
break;
}
}
}


But it don't put the focus on any select items.
I think field.elements.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
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 ;)

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top