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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

list box problems

Status
Not open for further replies.

moley

IS-IT--Management
Mar 26, 2002
95
GB
Hi,

I'm having problems getting a second list box to populate, The value is undefined in the second list. I think its how i'm trying to add the vaule option.
I'm just starting JS so any help would be great.

Thanks

<script language="JavaScript">
<!--
var shifts;

function init(){

//to initialize them all to the same times
shifts={dayOfWeek: [{day: "monday",theShifts: ["1","2","3"],theValues: [3,2,6]},{day:"tuesday",theShifts: ["4","2","5"],theValues:[3,2,6]}]};
}

//change day function
function changeDay(elem){
var j, day;
var shiftText;

if(elem.selectedIndex == 0) //if they selected our pretty [select a day] stmt
return false; //do nothing and leave quietly

//Clear the second drop down of all but top [select a shift]
for (i = document.frmFormName.workday.options.length - 1; i >= 1; i--) document.frmFormName.workday.options = null;
document.frmFormName.workday.selectedIndex = 0;

//grab day from select box
day = elem.selectedIndex;

for(j=0;j<shifts.dayOfWeek[day].theShifts.length;j++){
document.frmFormName.workday.options[j] = new Option(shifts.dayOfWeek[day].theShifts[j]);
document.frmFormName.workday.options[j] = new Option(shifts.dayOfWeek[day].theValues[j]);

document.frmFormName.workday.options[j].text = shifts.dayOfWeek[day].theShifts[j].text;
document.frmFormName.workday.options[j].value = shifts.dayOfWeek[day].theValues[j].value;


}
}
//-->
</script>
</script>
</head>
<body onLoad="init();">
<form method=POST action="wherever.html" name="frmFormName">

<select name="weekday" onChange="changeDay(this);">
<option value="0">[Select a day]</option>
<option value="1">Monday</option>
<option value="2">Tuesday</option>
</select>
<select name="workday" id="workday">
<option value="0">[Select a Shift]</option>
</select>
</form>
</body>
</html>
 
In your code useful selectedIndex starts from 1, while shift indexes start from 0 as usual. Plus last for() loop is kind of messy. Try something like:
Code:
function changeDay( oWeekDay ){
	var day = oWeekDay.selectedIndex-1;
	if( day< 0 ) return false;
    
	var oWorkDay = document.frmFormName.workday;
	while (oWorkDay.options.length >0)	oWorkDay.removeChild(oWorkDay.options[0]);

	for(j=0;j<shifts.dayOfWeek[day].theShifts.length;j++){
		oWorkDay.options[j] = new Option( shifts.dayOfWeek[day].theShifts[j], shifts.dayOfWeek[day].theValues[j] );
	}
}
 
Whoops... minor correction. To clear all but top option, change this:
Code:
		while (oWorkDay.options.length >[COLOR=red]1[/color])	oWorkDay.removeChild(oWorkDay.options[[COLOR=red]1[/color]]);

	for(j=0;j<shifts.dayOfWeek[day].theShifts.length;j++){
		oWorkDay.options[j[COLOR=red]+1[/color]] = new Option( shifts.dayOfWeek[day].theShifts[j], shifts.dayOfWeek[day].theValues[j] );
 
Thanks for the input. I managed to get something similar to your code which works fine now. But i am stuck on how to get
the recordset into this array. I wanted to hard code the values to get the array working first.

Code:
WorkTypeOption ={WorkTypeList: [{WorkType: "1", ssCategory: ["Outbound","Inbound","Written"],AssCategoryID: "1","2","3"]}, {WorkType: "2", AssCategoryID: ["Outbound"]}]};

Any ideas??? Thanks
 
There is one "[" missing after AssCategoryID.
Perhaps you can use associative arrays... they are OK for name/value pairs:
Code:
	WorkTypes = new Array();
	ac = new Array();
	ac["1"] = "Outbound";
	ac["2"] = "Inbound";
	ac["3"] = "Written";
	WorkTypes["1"] = ac;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top