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

getting the values of dynamically-named pulldown menus

Status
Not open for further replies.

togura

Programmer
Joined
Feb 5, 2006
Messages
2
Location
GB
Hi

I'm a longtime java coder but javascript newbie. got a quick problem i hope someone can help with. I am creating a number of pulldown menus, the names of which are created on-the-fly:

Code:
for (i = 0; i < outcomes.length; i++) {
   ...
   for (j = 0; j < factors.length; j++) {
      document.write ('<td> <select id=\"s' + i + 's' + j + '\" name=\"n' + i + 's' + j + '\" size=1><option value=\"0\">&nbsp;</option><option value=\"5\">Highly Desirable</option>...</select></td>  ');
   }
}

so basically, if the user has previously chosen to have 3 outcomes and 2 factors, the code above will produce pulldown menus with id's of s0s0, s0s1, s0s2, s1s0, s1s1, s1s2. As you can see, if the outcome and factor arrays are different lengths, different numbers of pulldown menus will be created.

so - the problem is this. how do i determine what value the user has chosen for a specific pulldown menu? i understand i could write something like this:

Code:
document.NavForm.s0s0.options [document.NavForm.s0s0.selectedIndex].value;

to get the value of pulldown menu s0s0. BUT this hardwires the name of the pulldown menu. i won't know beforehand how many pulldown menus the user has (as they are rendered dynamically) - so i won't know, for instance, if there is a pulldown menu s3s4. I've tried code along these lines, creating a multi-dimensional array then using a for loop to access values thus:

Code:
row0dropDowns = ['s0-s0','s0-s1','s0-s2','s0-s3','s0-s4','s0-s5','s0-s6'];
row1dropDowns = ['s1-s0','s1-s1','s1-s2','s1-s3','s1-s4','s1-s5','s1-s6'];
...
columnDropDowns = [row0dropDowns, row1dropDowns, ...];

document.all[columnDropDowns[m][m]].options[document.all[columnDropDowns[m][m]].selectedIndex].value;

but this gives the error 'columnDropDowns is null or not an object' or similar.

so - the big question - how do i access the values of pulldown menus which i have created - and thus named - on the fly? any help gratefully received, and also, if you need any clarification, let me know.

andy

p.s. i've crossposted this on webdeveloper, fyi
 
Maybe I'm missing something here... you already have a loop to create the menus (from known counts), so why not simply use the same loops to read the values back?

Dan



[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
I am planning on using a loop to read the results back, but at the moment i don't know how to reference the pulldown menu objects that were created when the page was rendered. the 'create' loops are used when the page is rendered, whereas the 'read value' loops will be used when a user presses a PROCESS button.

the basic problem is - how do i get my code to say "right, what's the value of the fourth pulldown menu in the first row of my table? to get that value, i need the id of the menu - hmm, how do i get that id? it was created on the fly after all, so i don't know offhand what its id is..." give me an answer to that question and you've solved my problem!
 
You can establish the menu list in an array like this under the assumption that the series is dynamically established without "hole" with in indices.
[tt]
var menulist=new Array();
var i,j;
while (true) {
i=0;
while (true) {
j=0;
if (document.NavForm.elements["s"+i+"s"+j]) {
menulist.push("s"+i+"s"+j);
j++;
} else {
break;
}
}
if (j==0) {
break;
} else {
i++;
}
}
[/tt]
(Can be made shorter in number of lines, but that's unimportant.)
 
I'd go with:

Code:
var s = '';
for (var i=0; i<outcomes.length; i++) {
	for (var j=0; j<factors.length; j++) {
		var selBox = document.forms['NavForm'].elements['n' + i + 's' + j];
		s += 'Select box ' + selBox.name + ' has option ' + selBox.selectedIndex + ' chosen.\n';
	}
}
alert(s);

Hope this helps,
Dan

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top