[Javascript] Programatically build object names

Paul Novitski paul at novitskisoftware.com
Wed Jul 6 16:03:27 CDT 2005


Glenn,

In your Doesn't Work code-block, this looks like the problem to me:

>         var SelectName = 'document.frmTranscript.Select1' + iNumber + 'To';
>         var selectbox = document.getElementById(SelectName);

getElementById() is expecting a simple id, not the full DOM parentage of 
the element.  I'd code it like this:

         var selectbox = document.getElementById('Select1' + iNumber + 'To');

Paul


At 01:23 PM 7/6/2005, 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