Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Window location and focus problem

Status
Not open for further replies.

Fingerprint

Programmer
Oct 21, 2002
35
GB
I'm working on a very large form that has to be written in client-side javascript and HTML, and I thought I'd nearly finished.
Then I was asked if after a validation error pop-up, the program could move to the question, AND focus on one of the fields associated with it, but I can't work out if it's possible. I can do either:

Code:
 if (!validRequired(form1.jobtitle))
  {
  alert ('Q1: Please enter a "Job Title".');
  window.location='#Quest1';
  return false;
  }

OR

Code:
 if (!validRequired(form1.jobtitle))
  {
  alert ('Q1: Please enter a "Job Title".');
  form1.jobtitle.focus();
  return false;
  }

But both doesn't seem to work! Does anybody have any ideas?
 

seems as it should work...

post the code.

- spewn
 
Well - since all the code has to go in one document (don't ask) and there're 61 fields, it's huge.

Code:
function validateForm(form1)
{
 if (!validRequired(form1.jobtitle))
  {
  alert ('Q1: Please enter a "Job Title".');
  window.location='#Quest1';
  form1.jobtitle.focus();
  return false;
  }

That's the part of the validation function

Code:
<!-- Start of form -->
<table bgcolor=&quot;#E6E6FA&quot; border=&quot;0&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; width=&quot;100%&quot;>
<tr>
<td>
<form name=&quot;form1&quot; id=&quot;form1&quot;>

<!--Question 1 -->
<a name=&quot;Quest1&quot;></a>
<p>
<font color=&quot;#000000&quot; size=&quot;+1&quot;><b>
1. Please provide employee and job details
</b></font>
</p>

<p>
We need to follow this job in subsequent quarters, so please provide sufficient detail to enable you to identify this <b>job</b> in the future.
<br>
If the pay for this job is related to the age or length of service of the occupant, specify the age/length as part of the grade, e.g. Age 17 or 3 years.
<br>
If the employee is a trainee or apprentice, please specify this in the grade, e.g. apprentice, 1st year.
<br>
<br>
</p>

<table bgcolor=&quot;#E6E6FA&quot; border=&quot;0&quot; cellpadding=&quot;3&quot; cellspacing=&quot;0&quot;>
<tr>
<td>
 <table bgcolor=&quot;#E6E6FA&quot; border=&quot;0&quot; cellpadding=&quot;3&quot; cellspacing=&quot;0&quot; wdith=&quot;100%&quot;>
 <tr>
 <td align=&quot;left&quot;>
 <b>Job Title</b>
 </td>
 <td>
 <input type=&quot;text&quot; name=&quot;jobtitle&quot; title=&quot;The space for your answer is not limited&quot;>
 </td>
 <td colspan=&quot;3&quot;>&nbsp;</td>
 </tr>
 <tr>
 <td align=&quot;left&quot;>
 <div title=&quot;To identify which employee is occupying the job&quot;><b>
 Employee ID
 </b></div>
 </td>
 <td>
 <input type=&quot;text&quot; name=&quot;empident&quot; maxlength=&quot;30&quot;>
 </td>
 <td width=&quot;*&quot;>&nbsp;</td>
 <td align=&quot;right&quot;>
 <div title=&quot;To identify the specific position being followed&quot;><b>
 Job/position ID
 </b></div>
 </td>
 <td align=&quot;right&quot;>
 <input type=&quot;text&quot; name=&quot;jobident&quot; maxlength=&quot;30&quot;>
 </td>
 </tr>
 <tr>
 <td align=&quot;left&quot;>
 <b>Grade/Class/Level</b>
 </td>
 <td>
 <input type=&quot;text&quot; name=&quot;grade&quot; maxlength=&quot;30&quot;>
 </td>
 <td width=&quot;*&quot;>&nbsp;</td>
 <td align=&quot;right&quot;>
 <b>Location</b>
 </td>
 <td align=&quot;right=&quot;>
 <input type=&quot;text&quot; name=&quot;location&quot; maxlength=&quot;30&quot;>
 </td>
 </tr>
 </table>

And that's what it'll be going to.

Code:
<td><input type=&quot;button&quot; value=&quot;Save entry&quot; name=&quot;Submitbutton&quot; onclick=&quot;if(validateForm(form1)){ save(form1) }else{ return false }&quot; style=&quot;background-color:#F0F8FF;border-color:#B0E0E6;&quot; title=&quot;Save this entry and move on to next employee.&quot;></td>

This button validates and saves it all.

When I test it out, IE moves up to the anchor ok, but doesn't focus on the field.
It should work, but for some reason doesn't.

- fingerprint
 
If you just use focus() to some field, the page will not scroll to it if it's not visible.
I think that you should add anchors near the form fields and use the 1st method (changed a bit):

if (!validRequired(form1.jobtitle))
{
alert ('Q1: Please enter a &quot;Job Title&quot;.');
window.location.href = window.location.href + '#Quest1'; // or just current page name with anchor name
form1.jobtitle.focus();
return false;
}
 
Ta - I'll try it and see, but for now I'm off home! I'll post the results tomorrow...

- fingerprint
 
Nope doesn't work.
Getting some wierd behaviour - the page moves up to the question and no focus is applied to anything, or the focus is applied to the field and you can't see the question.

- fingerprint
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top