1

I would like to know how to solve the following problem:

I have an array - in asp classic

 objArray

And I am using this in loop in javascript. The problem is how I can access the individual elements in the asp-array when I am in the javascript code, and using a variable for it. In Javascript I can easily get an individal element from the asp-array if I use an integer, for instance:

var theString = '<%=objArray[3]%>';

That is the element in the 4'th position.

But - int the loop in javascript - i need to use the variable 'i' to get the elements - but how can I do that since its asp? See the code below.

 <script type="text/javascript">

    var arrayLen = '<%=nObjects%>'

    for (var i = 0; i < arrayLen; i++) {

       var y = document.createElement("label");

       y.innerHTML = '<%=objArray(i)%>'; // this doesnt work since asp doesnt recognice the variable i

       document.body.appendChild(y);

     }

  </script>

2 Answers 2

2

Since you have the array at the server side, you could do the looping in the ASP code itself:

<%
Dim objArray : objArray = Array(1,2,3,4,5)
Dim i
%>
<script type="text/javascript">

var y;
<%
   for i=0 to UBound(objArray)
%>

       y = document.createElement("label");
       y.innerHTML = "<%=objArray(i)%>"; 
       y.id="label_<%=objArray(i)%>";
       document.body.appendChild(y);
<%
next
%>

     document.getElementById("label_1").innerHTML = "Modified First Label";

</script>
Sign up to request clarification or add additional context in comments.

1 Comment

Another approach (depending on your requirement) would be to pass the handling of the array and generation of the <label> to ASP via an AJAX call that way you won't end up with lots of client-side code being generated bloating your response.
-1

You missed in your code length:

<script type="text/javascript">

    var arrayLen = '<%=nObjects%>';

    for (var i = 0; i < arrayLen.length; i++) {

       var y = document.createElement("label");

       y.innerHTML = 'arrayLen(i)'; 

       document.body.appendChild(y);

     }

  </script>

1 Comment

Unfortunately that won't worked either because the i variable is unknown to Classic ASP at that point, the loop needs to be done server-side it can't be avoided (unless you serialise the Array to the client but that is just pointless imo), remember server-side code is set before anything is returned to the client.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.