java
java

Reputation: 1165

Use javascript variable in asp-classic

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>

Upvotes: 1

Views: 3314

Answers (2)

Flakes
Flakes

Reputation: 2442

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>

Upvotes: 2

Husni Salax
Husni Salax

Reputation: 2020

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>

Upvotes: -1

Related Questions