[Javascript] Programatically build object names

Flavio Gomes flavio at economisa.com.br
Wed Jul 6 15:45:33 CDT 2005


I think your problem would be easier to solve by using "getElementById" 
by itself. Can't you pass the object own ID to the function?

Or, maybe something like this

myObj = document.getElementById('Div' + iNumber+ 'Coll');
     //Assuming your using unique ID's thru the document
     //or
myObj = document.getElementsByName('Div' + iNumber+ 'Coll')[0]; 
     //the [0] here is because Get*Elements*ByName returns an array
     // of the objects matching the given name

Hope to Help,

-- 
Flavio Gomes
flavio at economisa.com.br



Glenn E. Lanier, II wrote:

>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