Forum Moderators: open

Message Too Old, No Replies

JavaScript Array

Unable to Access an Array

         

andreew

10:55 pm on Feb 18, 2004 (gmt 0)

10+ Year Member



I have CGI script that I am trying to use JavaScript to enchance by hiding parts of the script from the user until they complete certain actions. I am using perl to populate some java arrays. Then I want to use those arrays to create new options in the specified drop down menu box based upon the selection of the user from another menu box. The value of the variable I pass has the same name as the array I want to loop through to create the new options. But, as it stands now the only thing that happens is that it outputs the value of the
selected item and doesn't loop through the array of the same value. The code I'm using is below. Any suggestions or ideas would be appreciated. Thanks

<SELECT value="oldroom" onChange="makemenu(this.option(selectedIndex).value)">

function makemenu(elementid){
HTML
foreach (@building){
chomp $_;
print "\n\t\t\tvar $_ = new Array()\n";

$i=0;

$query9=$dbh->prepare("SELECT room FROM roomid WHERE building=\"$_\";");
$query9->execute;

while(@row9=$query9->fetchrow_array()){

foreach $room(@row9){
chomp $_;
print "\t\t\t$_\[$i]\=\"$room\";\n";
$i++;

}

}

}

print <<HTML;

//var j=1;

for(var i=0;i<elementid.length;i++){

alert(elementid[i]);

//document.forms[0].oldroom.options[j] = new Option(elementid[i],elementid[i]);

//j++;

}

}

DrDoc

11:30 pm on Feb 18, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Welcome to Webmaster World!

Don't know if you have named elements in the JS array... But, you cannot access the size (length) of a variable containing named elements without specifying the number of elements it has when creating it.


<script type="text/javascript">
foo = new Array();
foo['bar'] = "test";
foo['baz'] = "widget";

bar = new Array(2);
bar['bar'] = "test";
bar['baz'] = "widget";

baz = new Array();
baz[0] = "test";
baz[2] = "widget";

widget = new Array(1);
widget[0] = "test";
widget[199] = "widget";

blah = new Array(50);
blah[0] = "test";
blah[2] = "widget";
alert("'foo' has " + foo.length + " elements\n'bar' has " + bar.length + " elements\n'baz' has " + baz.length + " elements\n'widget' has " + widget.length + " elements\n'blah' has " + blah.length + " elements");
</script>

andreew

11:51 pm on Feb 18, 2004 (gmt 0)

10+ Year Member



Thanks DrDoc. I'll be sure to specify the length. But I don't think that's what I'm after. For example. If a user selects an option from the drop down menu box with the value of A, then A is passed to the function. But when I try to loop through a JS array of the same name(in this case A) the only thing being printed out is A not the value of the array. Do you think that not specifying the length would cause the problem I'm having?

andreew

1:20 am on Feb 20, 2004 (gmt 0)

10+ Year Member



I was able to solve the problem myself. The problem was I was trying to pass a variable name and then use the value of that variable to call an array of the same name. JS didn't like that. To solve the problem I made an associtive array of arrays. I made the key the value I want to pass from the drop down menu box, and the array that key pointed to was the information I wanted to populate the second drop down menu box with. Below is the code I used.

function makemenu(elementid){

rooms=new Array()
HTML
foreach (@building){

$value="";
$query9=$dbh->prepare("SELECT room FROM roomid WHERE building=\"$_\";");
$query9->execute;
while(@row9=$query9->fetchrow_array()){

foreach $room(@row9){

push(@tmp, $room);
$temp="\"$room\"";
$value=$value."$temp,"

}

}

chop $value;
print "\t\t\trooms[\"$_\"]=new Array($value);\n";
undef @tmp;

}
print <<HTML;
var j=1;
var i;

for(i=0;i<rooms[elementid].length;i++){

document.forms[0].oldroom.options[j] = new Option(rooms[elementid][i],rooms[elementid][i],false,false);
j++;

}

}