Filling a select menu from associative array

D

david

Hello all,

on a web page, i use php to create several associative arrays in javascript:

subTab1 = new Object();
subTab1["key1"] = "value1";

subTab2 = new Object();
subTab2["key1"] = "value1";


and i have two select menu:

<select name="selectMenu1" onChange="javascript:type_changed()"
 
D

david

fucking mozilla, my post got cut, sorry

Hello all,

on a web page, i use php to create several associative arrays in javascript:

subTab1 = new Object();
subTab1["key1"] = "value1";

subTab2 = new Object();
subTab2["key1"] = "value1";


and i have two select menu:

<select name="selectMenu1" onChange="javascript:type_changed()">
<option value="subTab1">Category 1</option>
<option value="subTab2">Category 2</option>
</select>

<select name="selectMenu2">
</select>

and i have the following function in "almost" code ;) :

function type_changed()
{
type = document.theForm.selectMenu1.value;

// object name to object
tab = document[type];

dst = document.theForm.selectMenu2;

// clear the menu
for (var i = 0; i < dst.length; i++)
dst.options = null;

for (var i = 0; i < tab.length; i++)
{
dst.options = new Option(tab.value, tab.key);
}
}

If somebody could help me to finalyse the function :)

Thx by advance
 
D

david

david wrote:

it's done

function type_changed()
{
var type = document.theForm.selectMenu1.value;

// object name to object
var tab = g[type];

var dst = document.theForm.selectMenu2;

// clear the menu
for (var i = 0; i < dst.length; i++)
dst.options = null;

i = 0;
for (key in tab)
{
dst.options = new Option(tab[key], key);
i++;
}
}
 
L

Lasse Reichstein Nielsen

david said:
david wrote:

it's done ....
var dst = document.theForm.selectMenu2;

I recommend
var dst = document.forms['theForm'].elements['selectMenu2'];
but that is mostly a matter of taste.
// clear the menu
for (var i = 0; i < dst.length; i++)
dst.options = null;


This clearing fails. Try insterting an
alert(dst.length);
after the loop and see that approx. half of the options are still
there. This is because setting an option to null will remove it,
which moves the next option into its place and reduces the length.

A better solution is
dst.length = 0;

/L
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top