Click to See Complete Forum and Search --> : testing for the visible form


LuigiX
10-14-2003, 05:23 PM
Hi

I have three forms - newEmp, transferEmp and leavingEmp.

Form visibility is set from a group of option buttons - only one form can be visible at one time.

I need to assign the "theForm" attribute to the currently visible form so I can set the combo boxes up on the visible form.

At present the code works on only one form - the newEmp form as follows:

theform=document.newEmp


Cheers

Luigi

Khalid Ali
10-14-2003, 06:58 PM
If I understand you correctly,you will need to set style="visibility:hidden" for 2 form elements to begin with and 1 to be visible.
then you can programatically access the form elements style props and change the as required

LuigiX
10-14-2003, 07:17 PM
Hi Kalid

Basically its setting the "theform" property using something like the following.

if (document.employmentForm.visibility ="visible") {
theform=document.employmentForm;
} else if (document.transferEmp.visibility ="visible"){
theform=document.transferEmp;
} else if (document.leavingEmp.visibility ="visible"){
theform=document.leavingEmp;
}


Can you assist

Cheers

Luigi

Khalid Ali
10-14-2003, 07:27 PM
the comparison operator is 2 equal signes no 1,
1 equal sign is assignment operator.

if (document.employmentForm.visibility =="visible") {
theform=document.employmentForm;
} else if (document.transferEmp.visibility =="visible"){
theform=document.transferEmp;
} else if (document.leavingEmp.visibility =="visible"){
theform=document.leavingEmp;
}

for this to work the form must already have its style property
visibility
to be set

LuigiX
10-14-2003, 07:38 PM
Hi Khalid

Nearly there. Your code produces the error "tranfer.Emp.visibility is null or not an object"

My code to set the "hidden" property works like this:

function switchDiv(div_id)
{
var style_sheet = getStyleObject(div_id);
if (style_sheet)
{
hideAll();
changeObjectVisibility(div_id, "visible");
}
else
{
alert("sorry, this only works in browsers that do Dynamic HTML");
}
}


and "hideAll" is as follows:

function hideAll()
{
changeObjectVisibility("newEmp","hidden");
changeObjectVisibility("transferEmp","hidden");
changeObjectVisibility("leavingEmp","hidden");
}

Thanks again

Luigi

Khalid Ali
10-14-2003, 08:02 PM
code produces error means there is somethign wrong with your html declarations or you are not applying the solution as suggested.

here is the proper way in case you missed it in earlier post

I just copied earlier your code the, which was wrong since you missed style in it. correct code should be as above.

document.employmentForm.style.visibility =="visible"


if there more/still errors then post the link to the page where you have the code,or post the complete code here

LuigiX
10-14-2003, 10:02 PM
Hi

Here's the code. You'll see the drop downs work fine with the newEmp form but not with the transferEmp form.

Many thanks for your help


<html>

<head>
<meta name="GENERATOR" content="Microsoft FrontPage 5.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>Staff Movements</title>

<SCRIPT language="JavaScript">

function switchDiv(div_id)
{
var style_sheet = getStyleObject(div_id);
if (style_sheet)
{
hideAll();
changeObjectVisibility(div_id, "visible");
}
else
{
alert("sorry, this only works in browsers that do Dynamic HTML");
}
}

function getStyleObject(objectId) {
// checkW3C DOM, then MSIE 4, then NN 4.
//
if(document.getElementById && document.getElementById(objectId)) {
return document.getElementById(objectId).style;
}
else if (document.all && document.all(objectId)) {
return document.all(objectId).style;
}
else if (document.layers && document.layers[objectId]) {
return document.layers[objectId];
} else {
return false;
}
}



function changeObjectVisibility(objectId, newVisibility) {
// first get the object's stylesheet
var styleObject = getStyleObject(objectId);

// then if we find a stylesheet, set its visibility
// as requested
//
if (styleObject) {
styleObject.visibility = newVisibility;
return true;
} else {
return false;
}
}

function hideAll()
{
changeObjectVisibility("newEmp","hidden");
changeObjectVisibility("transferEmp","hidden");
changeObjectVisibility("leavingEmp","hidden");
}

datasets=new Array();
datasets[0]="Manager,John Smith,Alex Misiw,Jeff Watson,Bruno Fish".split(",");
datasets[1]="Delegate,John Paul,Jeff Paul/Wiley Mason,Lisa Presley".split(",");

/* --------------------------------------------------------------------
populateselect()
* --------------------------------------------------------------------
* This function is to be called onload and populates the form according
* to the datasets.
*/

function populateselect(){
/* Define Form elements (lesser typing)
* employmentForm is the form name, select1 and select 2 the selectboxes.
* Change this to your names in the HTML document
*/

theform=document.employmentForm;

sel1 = theform.Manager_Delegate;
sel2 = theform.Completed_By;

sel1.options.length = 0;
sel1.options.length = datasets.length;
for (i=0;i<datasets.length;i++){
sel1.options[i].text = datasets[i][0];
sel1.options[i].value = datasets[i][0];
}
sel1.selectedIndex=0;
changeselect(0);

}


/* --------------------------------------------------------------------
changeselect()
* --------------------------------------------------------------------
* This function changes the content of the second selectbox according
* to the data of the datasets and the selected option of the first.
*/

function changeselect(){
/* Define Form elements (lesser typing)
* employmentForm is the form name, select1 and select 2 the selectboxes.
* Change this to your names in the HTML document
*/

sel1 = theform.Manager_Delegate
sel2 = theform.Completed_By

dataset=sel1.selectedIndex;
sel2.options.length = 0;
sel2.options.length = datasets[dataset].length-1;
for (i=1;i<((datasets[dataset].length));i++){
sel2.options[i-1].text = datasets[dataset][i];
sel2.options[i-1].value = datasets[dataset][i];
}
sel2.selectedIndex=0;
}


</script>

</head>

<body onload="populateselect()">
<p><b><font size="6">Employee Movements</font></b> </p>
<p><i>Authorising Manager or Delegate only to complete this form. Prior to
purchase of equipment or vehicle, the Authorising Manager will be contacted to
validate the purchase. PC's will be set up with Microsoft Office, Outlook and
the business applications selected. Please specify any other specific software
requirements.</i></p>
<hr>
<table id="AutoNumber1" style="BORDER-COLLAPSE: collapse" borderColor="#111111" height="99" cellSpacing="0" cellPadding="0" width="400" border="0">
<tr>
<td height="33"><b>Select Category:</b></td>
</tr>
<tr>
<td height="21">
<input onclick="switchDiv('newEmp');" type="radio" CHECKED value="newEmp" name="form_type">
New Employee</td>
</tr>
<tr>
<td height="21">
<input onclick="switchDiv('transferEmp');" type="radio" value="transferEmp" name="form_type">
Transferring</td>
</tr>
<tr>
<td height="21">
<input onclick="switchDiv('leavingEmp');" type="radio" value="leaving" name="form_type">
Leaving</td>
</tr>
</table>

<div id="transferEmp" style="position:absolute;top:275;left:5;visibility:hidden">
<form name=transferEmploymentForm method="POST" action="--WEBBOT-SELF--">
<table border="0" cellpadding="0" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" width="500" id="AutoNumber2">
<tr>
<td align="right" width="170">Completed By:</td>
<td width="330">
<select name="Manager_Delegate" onchange="changeselect()" tabindex="2">
</select></td>
</tr>
<tr>
<td align="right" width="170">Name:</td>
<td width="330">
</select>
<select name="Completed_By" tabindex="3">
</select><br>
</td>
</tr>
</table>
<br>
<br>
<p><input type="submit" value="Submit" name="Form_Sent"><input type="reset" value="Reset" name="B2"></p>
</form>
</div>

<div id="newEmp" style="position:absolute;top:275;left:5;visibility:hidden">
<form name=employmentForm method="POST" action="--WEBBOT-SELF--">
<table border="0" cellpadding="0" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" width="500" id="AutoNumber2">
<tr>
<td align="right" width="170">Completed By:</td>
<td width="330">
<select name="Manager_Delegate" onchange="changeselect()" tabindex="2">
</select></td>
</tr>
<tr>
<td align="right" width="170">Name:</td>
<td width="330">
</select>
<select name="Completed_By" tabindex="3">
</select><br>
</td>
</tr>
</table>
<br>
<br>

<br>
<p><input type="submit" value="Submit" name="Form_Sent"><input type="reset" value="Reset" name="B2"></p>
</form>
</div>
<br>

&nbsp;</body></html>