471,316 Members | 1,444 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,316 software developers and data experts.

[Web] Focus on first visible control

I have a Web form with a number of text boxes. If the user submits the
page with some text boxes filled in, those then become plain text
(with accompanying <input type="hidden"> fields for when the page is
resubmitted) - so the number of text boxes varies.

I was using this to focus on the first text box:

<body onLoad="document.forms[0].elements[0].focus();">

But it doesn't work when the first text box has been removed. In
effect, the browser is setting the focus to the new first control
(with type="hidden") so the caret is not visible.

How can I focus on the first *visible* input element?

P.
Jul 23 '05 #1
5 3234
you can test if the controle/element is of type == "text".

Jul 23 '05 #2
Lee
Paul E Collins said:

I have a Web form with a number of text boxes. If the user submits the
page with some text boxes filled in, those then become plain text
(with accompanying <input type="hidden"> fields for when the page is
resubmitted) - so the number of text boxes varies.

I was using this to focus on the first text box:

<body onLoad="document.forms[0].elements[0].focus();">

But it doesn't work when the first text box has been removed. In
effect, the browser is setting the focus to the new first control
(with type="hidden") so the caret is not visible.

How can I focus on the first *visible* input element?


Whatever server-side code you're using to create the hidden fields "knows" which
is the first visible field. Have it modify the onload handler accordingly.

Jul 23 '05 #3
Oh, I've found a solution myself.

I'm using the 'type' property to check that an input is 'text' (and
not, say, 'hidden').

function focusFirstVisibleInput()
{
var d = document.forms[0];
for (var i = 1; i <= d.elements.length; i++)
{
var e = d.elements['a' + i];
if (e.type == 'text')
{
e.focus();
break;
}
}
}

P.
Jul 23 '05 #4
Paul E Collins wrote:
Oh, I've found a solution myself.

I'm using the 'type' property to check that an input is 'text' (and
not, say, 'hidden').

function focusFirstVisibleInput()
{
var d = document.forms[0]; for (var i = 1; i <= d.elements.length; i++)


I would start from 0(zero).
Mick

Jul 23 '05 #5
Lee
Mick White said:

Paul E Collins wrote:
Oh, I've found a solution myself.

I'm using the 'type' property to check that an input is 'text' (and
not, say, 'hidden').

function focusFirstVisibleInput()
{
var d = document.forms[0];

for (var i = 1; i <= d.elements.length; i++)


I would start from 0(zero).


and end at the last element, even though you should
never actually get past it in this application:

for (var i = 0; i < d.elements.length; i++)

Jul 23 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Melissa | last post: by
reply views Thread by Stephen Bedford | last post: by
5 posts views Thread by Glenn T. Kitchen | last post: by
1 post views Thread by Praveena.Alapati | last post: by
3 posts views Thread by Simon Tamman | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.