Andrew
Andrew

Reputation: 7778

get ArrayList JSP variables in JavaScript function

I am trying to get familiar with javaScript and currently stumbled upon one issue: How to get ArrayList JSP variables in JavaScript function?

Fo example I figured out how I can get an int value, but dont know how to move the array list

var total='<%=counter%>'; //convert jsp to JavaScript

Upvotes: 3

Views: 10624

Answers (2)

dku.rajkumar
dku.rajkumar

Reputation: 18568

There is no direct way to convert an ArrayList to javascript array, you can loop through the ArrayList and add each element to javascript array. try this

javascript:

var jsArray = [];
<%for(int i=0;i<arrayList.size();i++){%>
    jsArray.push("<%= arrayList.get(i)%>");
<%}%>

Upvotes: 6

Liam
Liam

Reputation: 2837

You need to use javascript array and create the array using values from the server

var myArray=new Array("Value1","Value2","Value3");//condensed array
var myArray=["Value1","Value2","Value3"];//literal array

You can also use the JSON format to get the literal from the server.

Upvotes: 1

Related Questions