0

I am trying to append a ArrayList to a JavaScript String variable using foreach loop but for some reason it doesn't seem to work. I have been trying to fix this problem for 2 days, but no luck yet. Can anyone help with this? Here's the code.

<div id="graphdiv"></div>

<script type="text/javascript">
    var s = "";
    <c:forEach items="${dateAndWaitTimes}" var="item">
        s.append(${item});
    </c:forEach>
    g = new Dygraph(document.getElementById("graphdiv"), s);
</script>

Here dateAndWaitTimes contains the ArrayList of String, returned by the controller.

-

Output that I am getting:

enter image description here

Expected Output:

enter image description here

5
  • 1
    you can't mix variables of 2 languages like that. Each language runs in different place at different time Commented Jul 12, 2016 at 17:42
  • Thanks,So is there any way to append the values of Java Arralylist<String> to a JavaScript string? Commented Jul 12, 2016 at 17:46
  • 1
    stackoverflow.com/questions/3040645/… Commented Jul 12, 2016 at 17:47
  • PM 77-1: I guess I am doing the same thing? Commented Jul 12, 2016 at 17:49
  • Try declaring "s =new Array();" and then s.push() instead of s.append() Commented Jul 12, 2016 at 17:57

1 Answer 1

0

It seems you're getting array values correctly but nothing display correct. one solution might be : put Java arraylist values into JS arrayList and then print JS arrayList values.

Code :

<script language="javascript">
$(function(){
    var values = new Array();        
    <c:forEach var="item" items="${dateAndWaitTimes}" varStatus="status">
        values.push("${item}");   
    </c:forEach>
    alert(" VALUES => "+values[0]);
    alert(" VALUES => "+values[1]);
    //alert(" VALUES => "+values[2]);

});

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

4 Comments

this should work. this is using JQuery so you should add it to your js file. or i can change to JS it for you as soon as possible.
Mohammad Reza Khatami, can you please change it to JS?
you can delete $function() then it doesn't work with JQuery anymore but i'm not sure in that, try to do it and if it doesn't work, put JQuery api in script src at top of your current page. good luck!
Thanks, I'll try that.

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.