1

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?

5
  • 1
    Sorry, but I'm not using JQuery. Commented Aug 16, 2011 at 17:57
  • Doesn't matter jQuery is JavaScript is as well. The solution I gave there can be applied to your problem too. Commented Aug 16, 2011 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. Commented Aug 16, 2011 at 18:18
  • @Brian: Keeping in mind that jQuery is a library and JavaScript a language, your C++/C and HTML/XML analogies are not correct imo. Anyway, that is just the way I express myself. If I have a function foo written in JavaScript, I also say, "this is a JavaScript function" or "this function is JavaScript". Maybe it's because I'm not a native English speaker. I'm sorry if it causes any confusion. Commented Aug 16, 2011 at 18:36
  • 2
    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 :) Commented Aug 16, 2011 at 18:40

1 Answer 1

4

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

Sign up to request clarification or add additional context in comments.

4 Comments

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
it's the same - you just have to define the array in javascript, and then fill it
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?
the page is generated by JSP once, and then sent to the client, where the javascript is interpreted.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.