In the following code I want to parse an array from ASP to a Javascript array, choose an array index in Javascript and output the value of the index in a div tag on a press of a button. This piece of coding is only one part of a larger code and i need to figure out this part to move on to the other that will also be in Javascript.
Here is the code in a .asp file:
<%
Dim va(3)
va(0) = 2
va(1) = 3
va(2) = 4
%>
<html>
<button onclick="func()">Press</button>
<div id="mydiv"></div>
<script src="jquery-1.10.2.min.js"></script>
<script>
function func(){
ba = new Array();
i = 0;
while(i < 3){
ba[i]=<%=va(i)%>;
i++;
}
document.getElementById("mydiv").innerHTML=ba[2];
}
</script>
</html>
Currently the code keeps on returning the value of the first index in the array, regardless of what index i choose in this line - document.getElementById("mydiv").innerHTML=ba[2];.
For instance the result should show 4, but it keeps giving me 2 in my div tag. If i replace the while loop with:
ba[0]=<%=va(0)%>;
ba[1]=<%=va(1)%>;
ba[2]=<%=va(2)%>;
it works, but i will be having much bigger arrays in the future to deal with. I also tried a for loop, ba = new Array(<%=va%>);, but no luck. How can i use a loop to do this. It seem very simple but i can't seem to get it to work, any advice is much appreciated. Thanks.