Focus()

S

Simon Wigzell

When I validate a form on submission I use the javascript focus function to
scroll the page to the location of the form error e.g.

document.[Form Name].[Field Name].focus();

If you happened to have the page scrolled above where the error field is
then the page will scroll so that the error field is at the very top of the
window. If you happened to have the page scrolled below where the error
field is then the page will scroll so that the error field is at the very
bottom of the window. Neither of these is very desirable. Any way to get the
error field to the middle of the window?

e.g. in javascript after the focus statement, can one test the location of
the error field and then either add half a windows worth of pixels to it if
it is at the top of the screen or subtract half a windows worth of pixels to
it if it is at the bottom of the screen?

Thanks!
 
S

Stephen Chalmers

Simon Wigzell said:
When I validate a form on submission I use the javascript focus function to
scroll the page to the location of the form error e.g.

document.[Form Name].[Field Name].focus();

If you happened to have the page scrolled above where the error field is
then the page will scroll so that the error field is at the very top of the
window. If you happened to have the page scrolled below where the error
field is then the page will scroll so that the error field is at the very
bottom of the window. Neither of these is very desirable. Any way to get the
error field to the middle of the window?

e.g. in javascript after the focus statement, can one test the location of
the error field and then either add half a windows worth of pixels to it if
it is at the top of the screen or subtract half a windows worth of pixels to
it if it is at the bottom of the screen?

Thanks!
You could place one or more anchors at strategic points above the relevant fields:

<A name='fieldPoint'> </A>

then send the page there on error:

document.location='#fieldPoint';
 
S

Simon Wigzell

Stephen Chalmers said:
Simon Wigzell said:
When I validate a form on submission I use the javascript focus function
to
scroll the page to the location of the form error e.g.

document.[Form Name].[Field Name].focus();

If you happened to have the page scrolled above where the error field is
then the page will scroll so that the error field is at the very top of
the
window. If you happened to have the page scrolled below where the error
field is then the page will scroll so that the error field is at the very
bottom of the window. Neither of these is very desirable. Any way to get
the
error field to the middle of the window?

e.g. in javascript after the focus statement, can one test the location
of
the error field and then either add half a windows worth of pixels to it
if
it is at the top of the screen or subtract half a windows worth of pixels
to
it if it is at the bottom of the screen?

Thanks!
You could place one or more anchors at strategic points above the relevant
fields:

<A name='fieldPoint'> </A>

then send the page there on error:

document.location='#fieldPoint';
Thanks, that syntax is new to me, that will do it.
 
R

RobG

Stephen said:
Simon Wigzell said:
When I validate a form on submission I use the javascript focus function to
scroll the page to the location of the form error e.g.

document.[Form Name].[Field Name].focus();

If you happened to have the page scrolled above where the error field is
then the page will scroll so that the error field is at the very top of the
window. If you happened to have the page scrolled below where the error
field is then the page will scroll so that the error field is at the very
bottom of the window. Neither of these is very desirable. Any way to get the
error field to the middle of the window?

e.g. in javascript after the focus statement, can one test the location of
the error field and then either add half a windows worth of pixels to it if
it is at the top of the screen or subtract half a windows worth of pixels to
it if it is at the bottom of the screen?

Thanks!

You could place one or more anchors at strategic points above the relevant fields:

<A name='fieldPoint'> </A>

then send the page there on error:

document.location='#fieldPoint';

Anchors can be created using element ids, A elements aren't required.

It may be useful to use the id of the form as an anchor or the id of
some fieldset inside the form.

<form id="formA" ...>
<!-- controls for for formA -->
</form>

<a href="#formA">Scroll to formA</a>








[...]
 

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
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top