[Javascript] Programatically build object names

Glenn E. Lanier, II glenn_lanier at netzero.net
Wed Jul 6 16:03:40 CDT 2005


Thanks for the getElementsByName()[0] tip. That is what works. These elements
don't have ID's assigned to them; is one method preferred over the other?

> -----Original Message-----
> From: javascript-bounces at LaTech.edu 
> [mailto:javascript-bounces at LaTech.edu] On Behalf Of Flavio Gomes
> Sent: Wednesday, July 06, 2005 3:46 PM
> To: [JavaScript List]
> Subject: Re: [Javascript] Programatically build object names
> 
> 
> 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
> >  
> >
> _______________________________________________
> Javascript mailing list
> Javascript at LaTech.edu
> https://lists.LaTech.edu/mailman/listinfo/javascript
> 




More information about the Javascript mailing list