HOWTO: Iterating Script Objects Using JavaScript

ID: Q197968


The information in this article applies to:


SUMMARY

In some cases, you may find it convenient to loop through a large number of script objects created by Design-Time Controls (DTCs). Although you cannot index these objects in an array, you can use the JavaScript eval function directly or indirectly to achieve the same result. This article demonstrates how you can loop through the script objects using JavaScript.


MORE INFORMATION

The following code constructs the three textbox script objects in a loop and then initializes them in a loop. (You may find it easier to convert a Textbox DTC to runtime text permanently first and customize its runtime code.)


   <!--#INCLUDE FILE="_ScriptLibrary/TextBox.ASP"-->
   <SCRIPT LANGUAGE=JavaScript RUNAT= Server>
   function _initTextbox1()
   {
     for (i=1; i<=3; i++)
      {
      eval('Textbox' + i + '.setStyle(TXT_TEXTBOX);')
      eval('Textbox' + i + '.setMaxLength(20);')
      eval('Textbox' + i + '.setColumnCount(20);')

      }
   }

   function _Textbox1_ctor()
   {
      for (i=1; i<=3; i++)
      CreateTextbox('Textbox' + i, _initTextbox2, null);

   }
   </script>

   <%
      for (i=1; i<=3; i++)
      eval ('Textbox' + i + '.display();')
   %> 
The following code segment clears the three textboxes in a loop:

   <SCRIPT ID=serverEventHandlersJS LANGUAGE=javascript RUNAT=Server>

   function Button1_onclick() {
   var i;
   for (i=1; i<=3; i++)
   eval("Textbox" + i + ".value='';")
   }
   </SCRIPT> 
Since eval is a JavaScript intrinsic function and not available in VBScript, you will have to use it indirectly if you are programming in VBScript. To clear the three textboxes in a loop in VBScript, you may use the following code, which introduces an additional function evalit:

   <SCRIPT ID=serverEventHandlersVBS LANGUAGE=vbscript RUNAT=Server>
   Sub Button1_onclick()
      Dim i
      For i = 1 to 3
         evalit "Textbox" & i & ".value = " & " 'howdy' "
      Next
   End Sub
   </SCRIPT>

   <SCRIPT LANGUAGE=javascript RUNAT=Server>

   function evalit(str)
   {
      eval(str);
   }
   </SCRIPT> 
This is similar to the sample given in JavaScript, except the VBScript code calls the eval method indirectly by calling the JavaScript function evalit.


Keywords          : kbJScript kbScript kbVisID600 kbGrpASP 
Version           : WINDOWS:6.0
Platform          : WINDOWS 
Issue type        : kbhowto 

Last Reviewed: May 7, 1999