4 Replies Latest reply on Jun 7, 2006 2:57 AM by blemmo

# Please help me simplify my code

i'm trying to make an application form for training courses for work where when you select a course from a combobox it automatically populates the date combobox from an array - i can get this to work but only in a very very long winded way:

var myListener bject = new Object();
title_cb.addEventListener("change", myListener);
myListener.change = function(){
if(title_cb.selectedItem == c1) {
date_cb.dataProvider=date1;
}
if(title_cb.selectedItem == c2) {
date_cb.dataProvider=date2;
}
if(title_cb.selectedItem == c3) {
date_cb.dataProvider=date3;
}
}
and so on..

can someone please show me how to simplfy this, i'm guessing i need a for loop but don't know how to get them to work.
• ###### 1. Re: Please help me simplify my code
var myListener bject = new Object();
title_cb.addEventListener("change", myListener);

// totalLenght = the number of items you need to loop through.

// 1�
////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////
// Assuming that "c1" and "date1" are strings
myListener.change = function () {
for (var i = 1; i<= totalLenght; i++){
( title_cb.selectedItem == "c" + i )? date_cb.dataProvider = "date"
+ i : null;

}
}

// 2�
////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////
// Assuming that "c1" and "date1" are vars in root

myListener.change = function () {
for (var i = 1; i<= totalLenght; i++){
( title_cb.selectedItem == _root["c" + i] )? date_cb.dataProvider =
_root["date" + i] : null;
}
}

"ab9hz" <webforumsuser@macromedia.com> wrote in message
news:e64bcm\$ctk\$1@forums.macromedia.com...
> i'm trying to make an application form for training courses for work where
> when
> you select a course from a combobox it automatically populates the date
> combobox from an array - i can get this to work but only in a very very
> long
> winded way:
>
> var myListener bject = new Object();
> title_cb.addEventListener("change", myListener);
> myListener.change = function(){
> if(title_cb.selectedItem == c1) {
> date_cb.dataProvider=date1;
> }
> if(title_cb.selectedItem == c2) {
> date_cb.dataProvider=date2;
> }
> if(title_cb.selectedItem == c3) {
> date_cb.dataProvider=date3;
> }
> }
> and so on..
>
> can someone please show me how to simplfy this, i'm guessing i need a for
> loop
> but don't know how to get them to work.
>

• ###### 2. Re: Please help me simplify my code
thanks, but when i try this the date combobox just shows "date20" (i've set the total length to 20) rather than a list of dates.

c1, c2,c3 etc are variables in root loaded held in an array and loaded into the title_cb combobox

date1, date2, date3 etc are names of arrays which need to be loaded into the date_cb combobox when the corresponding title is chosen in title_cb

anyone else got any ideas?
• ###### 3. Re: Please help me simplify my code
Hi,

try this:
for (var i = 1; i<= totalLenght; i++){
( title_cb.selectedItem == "c" + i )? date_cb.dataProvider = _root["date" + i] : null;
}

I'm not quite sure how the arrays get displayed that way. If you need certain array elements, use _root["date" + i][X], with X for the index to show.

hth,
blemmo
• ###### 4. Re: Please help me simplify my code
oops, just recognized that steve already posted this. Did you use this 2nd way he posted?