[Web] Focus on first visible control

P

Paul E Collins

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.
 
L

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.
 
P

Paul E Collins

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.
 
M

Mick White

Paul said:
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
 
L

Lee

Mick White said:
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++)
 

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

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top