[Javascript] Programatically build object names

Glenn E. Lanier, II glenn_lanier at netzero.net
Wed Jul 6 15:23:50 CDT 2005


I want to create a function that will take a number as the parameter, then build
the object name and use that to modify a value.

For example, if I have two select boxes (Select1To and Select2To), I want to
called a function (showSelection(1), showSelection(2)) that would display the
current selection in either box. Is this possible, or would I be better off to
pass the box name as a parameter (showSelection(1, Select1To))?

This works in FireFox:

Function showSelection()
{
	ShowLayer('div1Coll', false);

	var selectbox = document.frmTranscript.Select1To;
	var theValue = selectbox.options[selectbox.selectedIndex].value;
	alert(theValue);
}

But this does not:

function showSelection(iNumber)
{
	ShowLayer('div' + iNumber + 'Coll', false);

	var SelectName = 'document.frmTranscript.Select1' + iNumber + 'To';
	var selectbox = document.getElementById(SelectName);
	var theValue = selectbox.options[selectbox.selectedIndex].value;
	alert(theValue);
}

Thanks,
Glenn





More information about the Javascript mailing list