By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,317 Members | 1,290 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,317 IT Pros & Developers. It's quick & easy.

Filling a select menu from associative array

P: n/a
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()"

Jul 20 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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[i] = null;

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

If somebody could help me to finalyse the function :)

Thx by advance

--
David

Jul 20 '05 #2

P: n/a
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[i] = null;

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

--
David

Jul 20 '05 #3

P: n/a
david <pa*********@server.com> writes:
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[i] = 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
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.