skmansfield skmansfield -4 years ago 145
Javascript Question

Trouble passing HTML Integer array to Javascript function

Having trouble passing a JSP Integer array to javascript. The JSP array is correctly populated. I can correctly render elements of the JSP array in my JSP code. However, when I pass the JSP array to my javascript function the values are undefined in my javascript code.

JSP Code Snippet


<! This works correctly !>
<div class="col content-M">
<p>JSP Array Population Test</p>

<! This does not work !>
<div class="col content-L">
<p>Pass a JSP Integer Array to setDayValues</p>
<script type="text/javascript">setDayValues(${daynumbers});</script>
<div id="columnchart_values"></div>


JavaScript Snippet

var a = Array(6);

function setDayValues(values) {
a[0] = values[0];
a[1] = values[1];
a[2] = values[2];
a[3] = values[3];
a[4] = values[4];
a[5] = values[5];

function simpleColumnChart() {

var data = google.visualization.arrayToDataTable([
['Element', 'Density', { role: 'style' }],
['1', a[0], 'blue'],
['2', a[1], 'blue'],
['3', a[2], 'blue'],
['4', a[3], 'blue'],
['5', a[4], 'blue'],
['6', a[5], 'blue']



Like I said, a[x] is undifined in javascript.

Answer Source

I think the problem is the way you understand ${daynumbers} is wrong. what will ${daynumbers} return? a javascript array? try console.log(${daynumbers}), you will notice your problem, since you can see value of ${daynumbers[0]} , you probably returns a java Array Object which javascript won't understand, so you can try to convert ${daynumbers} to be a json string, and parse it.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download