[thelist] Javascript Error in Dynamic Select Script
dante
dante at vianet.net.au
Wed Aug 29 02:42:39 CDT 2001
G'Day All,
I've been using some javascript to fill in a select menu
& I've found a bug (well, sort of).
The code works fine except the value of the selected option is being sent
with my form as a boolean from an array.
I'm not sure where I got the script from, which makes it difficult to get
assistance in modifying it.
Can anyone see how I can fix this?
The header code:
-----------------------------------------------------------
var xStyle=new Array(2);;
var xVariant=new Array(5);;
//define objects for the main list
function ListItem(nvalue,description){
//function for defining the elements of the main list
this.nvalue=nvalue;
this.description=description;
}
//define objects for the dependent list
function ListSubItem(category,nvalue,description){
//function for defining the elements of the xVariants
this.category=category;
this.nvalue=nvalue;
this.description=description;
}
function PrepareData(){
// the function will fill in 2 arrays. The function can be filled in ASP
// so the values from the array will come from the database
xStyle[0]=new ListItem(0,'Fiction');
xStyle[1]=new ListItem(1,'Non-Fiction');
//Fill the values of the second list
//The first parameter is the category, the second is the value to be
returned
//from this selection and the third one is the text that appears in the
//combo box
xVariant[0]=new ListSubItem(0,'Book','Book');
xVariant[1]=new ListSubItem(0,'Graphic Novel','Graphic Novel');
xVariant[2]=new ListSubItem(0,'Magazine','Magazine');
xVariant[3]=new ListSubItem(1,'Book','Book');
xVariant[4]=new ListSubItem(1,'Magazine','Magazine');
}
function reFillList(){
var selValue;
var nOption;
selValue=upLoadLibrary.xStyle.value;
// alert('Selected value=' +selValue);
// clear the actual list by setting its length to 0
upLoadLibrary.xVariant.length=0
for (var i=0; i < xVariant.length;i++){
//fill the box with the values corresponding to
//the category in the first box
if (xVariant[i].category==selValue) {
nOption=upLoadLibrary.xVariant.length;
upLoadLibrary.xVariant.options[nOption]=new
Option(xVariant[i].description,xVariant[i].nvalue);
}
}
upLoadLibrary.xVariant.options[0].selected=true;
}
-----------------------------------------------------------
The xStyle select code:
-----------------------------------------------------------
<script language=javascript>
<!--
var page=""
var i;
// call the function that fills in the arrays
// so we'll use them to fill the select
PrepareData();
page+='<select name="xStyle" style="border:Opx; font-size:10px;"
onChange="reFillList()">';
for (i=0;i<xStyle.length;i++)
{
page+='<option value="'+ xStyle[i].nvalue +'"';
if (i==0) { page+=' selected '; }
page+='>' + xStyle[i].description + '</option>';
}
page+='</select>';
document.write(page);
//-->
</script>
-----------------------------------------------------------
The xVariant select code:
-----------------------------------------------------------
<select name="xVariant" style="border:0px; font-size:10px;">
<script language=javascript>
<!--
// since we have selected the first value in the main list,
// we have to fill this list
reFillList();
//-->
</script>
</select>
-----------------------------------------------------------
Thanks,
dante
Perth, Western Australia
dante at vianet.net.au
www.vianet.net.au/~dante
More information about the thelist
mailing list