Reputation: 13
I have an array of values that I want to pass in as a parameter to a Javascript function. For instance, something like:
<% ArrayList<String> arr = NodeUtil.getValues(); %>
<input type="button" name="submit" value="Add" onClick="addTextBox('<%= all values in arr %>')"/>
I'm trying to dynamically add textboxes to a div. Their names need to correspond to the values in the array. That is, in my js function I have a loop for:
newdiv.innerHTML = "<input type=\"text\" style=\"width: 235px\" name=\+each value in arr +"\" />
Is this possible?
Upvotes: 1
Views: 6625
Reputation: 597046
The solution in the linked question is good, but if you don't want an external library:
var array = new Array();
<c:forEach items="${jspArray}" var="item">
array.push("${item}");
</c:forEach>
This is ustil JSTL and EL, which is the recommended way of writing code in JSP. If you want to write scriptlets, it will be very similar - with a for (String item : arr) { .. }
Upvotes: 4