Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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?

share|improve this question
    
possible duplicate of passing array from program(javascript) to JQuery –  Felix Kling Aug 16 '11 at 17:55
1  
Sorry, but I'm not using JQuery. –  John Aug 16 '11 at 17:57
    
Doesn't matter jQuery is JavaScript is as well. The solution I gave there can be applied to your problem too. –  Felix Kling Aug 16 '11 at 18:00
1  
@Felix Kling - I hope that's not really waht you think "jQuery is JavaScript". This is like saying C++ is ATL or MFC, or that C++ is C, or that HTML is XML. jQuery is OF javaScript. jQuery IS NOT JavaScript. –  Brian Aug 16 '11 at 18:18
1  
Has very little to do with native speaking - you understood his objection to your issue. You also understood my analogies. HTML is a subset of XML. C++ is built on C. And on the language issue, saying jQuery is JavaScript is just like saying French, Italian and English are latin. They are all of latin. None of them is latin. Okay that may be a stretch, but I found it appropriate based on the discussion :) –  Brian Aug 16 '11 at 18:40

1 Answer 1

up vote 4 down vote accepted

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) { .. }

share|improve this answer
    
If I'm using scriplets, how would I pass in my arr to my javascript function? If I pass in arr to some javascript function test(arr) it complains about a cannot be resolved error. I'm not too familiar with the environment I'm working on, and it doesn't seem like JSTL is setup, but I'll try and work on that –  John Aug 16 '11 at 18:32
    
it's the same - you just have to define the array in javascript, and then fill it –  Bozho Aug 16 '11 at 18:37
    
But what if the values in the array are not set in stone? As in I call the function multiple times throughout my jsp where the values in the array are always changing? –  John Aug 16 '11 at 18:42
    
the page is generated by JSP once, and then sent to the client, where the javascript is interpreted. –  Bozho Aug 16 '11 at 18:48

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.