It appears you have not yet registered with DEVPPL. To register please click here... (it's fast, easy and free!)

Forum

Log In Sponsors
Board index Programming JavaScript Forum

Dependent Combo boxes

Dependent Combo boxes

Postby lomax0990 on Mon Oct 08, 2007 5:25 pm

Hello.

I am new to javascript and am working on dependent combo boxes. The way the original script was written it poped up an alert window with the description of what was selected. What I want to do is depending on what is selected in the "sublist" field I want to pop up a new window with a pdf and the pdf file depends on the week selected. (Ultimately I wnat different weeks listed in the sublist and whatever week is selected a pdf of the standings for that week will pop up. I have attached the code I presently have and what i've been trying to do. Any help is much appreciated.

<SCRIPT LANGUAGE=javascript>
<!--
var MainList=new Array(5);;
var SubList=new Array(13);;

//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 sublists
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

MainList[0]=new ListItem(0,"Red Crown");
MainList[1]=new ListItem(1,"Nascar");
MainList[2]=new ListItem(2,"Thur Mixec");
MainList[3]=new ListItem(3,"Independent");
MainList[4]=new ListItem(4,"YABA");
MainList[5]=new ListItem(5,"Seniors");

//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
SubList[0]=new ListSubItem(0,0,"RC Week 1");
SubList[1]=new ListSubItem(0,1,"RC Week 2");
SubList[2]=new ListSubItem(0,2,"RC Week 3");
SubList[3]=new ListSubItem(0,3,"RC Week 4");
SubList[4]=new ListSubItem(1,2,"Tomato");
SubList[5]=new ListSubItem(1,3,"Cucumber");
SubList[6]=new ListSubItem(2,4,"Male");
SubList[7]=new ListSubItem(2,5,"Female");
SubList[8]=new ListSubItem(3,6,"Georgina");
SubList[9]=new ListSubItem(3,7,"Susanne");
SubList[10]=new ListSubItem(4,8,"Peter");
SubList[11]=new ListSubItem(4,9,"Paul");
SubList[12]=new ListSubItem(5,10,"Amsterdam");
SubList[13]=new ListSubItem(5,11,"Paris");
}

function reFillList(){
var selValue;
var nOption;
selValue=form1.mainlist.value;
//alert("Selected value=" +selValue);
// clear the actual list by setting its length to 0
form1.sublist.length=0
for (var i=0; i < SubList.length;i++){
//fill the box with the values corresponding to
//the category in the first box
if (SubList[i].category==selValue) {
nOption=form1.sublist.length;
form1.sublist.options[nOption]=new Option(SubList[i].description,SubList[i].nvalue);
}
}
form1.sublist.options[0].selected=true;
}

function checkvalues()
{
var val1;
var val2;
val1=form1.mainlist.value;
val2=form1.sublist.value;

if (val2=0)
{
alert("You've selected Week 1")
}
else
{
alert("not week 1")
}
//show the selected values
// var val1;
// var val2;
// var cString;
// val1=form1.mainlist.value;
// val2=form1.sublist.value;
// cString="Main List=value:" + val1 + "-Description:"+MainList[val1].description
// cString+="\n"
// cString+="Sub List=value:" + val2+ "-Description:"+SubList[val2].description
// alert(cString);
}
//-->
</SCRIPT>

</HEAD>
<BODY>
<!-- for comments on this script , contact me at ruth@zahav.net.il -->
<h1>Dependent combo boxes sample</h1>

When selecting a value from the first combo box, the<BR>
second combo box will be filled in with new values<BR>
corresponding to the selection from the first combo box<BR>

<FORM name="form1">
<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 the main list: ";
page+="<SELECT NAME='mainlist' onChange='reFillList()'>";
for (i=0;i<MainList.length;i++) {
page+="<OPTION VALUE="+MainList[i].nvalue;
if (i==0) {
page+=" SELECTED ";
}
page+=">"+MainList[i].description;
}
page+="</SELECT>";
document.write(page);

//-->
</SCRIPT>
&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp
Select SubList:

<SELECT NAME='sublist' size=4>
<SCRIPT LANGUAGE=javascript>
<!--
// since we have selected the first value in the main list, we have to fill this list
reFillList();
//-->
</SCRIPT>

</SELECT>

<P>
<INPUT type="button" value="Press me" id=button1 name=button1 onClick="checkvalues()">
</FORM>


</BODY>
</HTML>
lomax0990
 
Posts: 0
Joined: Mon Oct 08, 2007 5:16 pm

Postby dafunkymunky on Sun Oct 14, 2007 4:14 pm

Thats simple.. I dunno why the code you have given above is not working as you said.. But what you want is easy to do.. Just add another variable that is the name of the pdf file in the sublist.. It must look something like this.

SubList[13]=new ListSubItem(5,11,"Paris","thisname.pdf");

onve you know which week is selected you can just use window.open() function to open the pdf file..

If you want a detailed solution, just reply.. But its better you try to work it out yourself, and if you are successful, just put the working code here..
--------DAFUNKYMUNKY--------
::THE::NECESSARY::[D]EVIL::
....errr i mean munky....
User avatar
dafunkymunky
100+ Club
 
Posts: 183
Joined: Fri Apr 08, 2005 7:32 am
Location: India

Postby dafunkymunky on Sun Oct 14, 2007 4:18 pm

Oh boy.. Oh boy.. I know whats wrong in this code..

[code]

if (val2=0)
{
alert("You've selected Week 1")
}

[\code]

you need double equal to inside the if

if(val2==0) whe you compare you need two equlas.. when you assign its one equal..
--------DAFUNKYMUNKY--------
::THE::NECESSARY::[D]EVIL::
....errr i mean munky....
User avatar
dafunkymunky
100+ Club
 
Posts: 183
Joined: Fri Apr 08, 2005 7:32 am
Location: India


Who is online

Users browsing this forum: No registered users and 5 guests