1

I have list object passed from java to jsp. I want to iterate that array list in my javascript function.

For example, I have list called "arrayList" in java and I am getting that in my jsp as

 <span id="valueList" style="display: none;">${arrayList}</span>

In javascript I tried fetching it as

$(document).ready(function () {
    var ranges = new Array();
    ranges.push($("#valueList").text());
    $.each(array, function(i, item) {
        console.log('val is '+item) 
    });
});

Here, in console, all the values are printing like [1,2,3,4,5,6] instead I want this to be printed separately.

Amiga500
  • 5,874
  • 10
  • 64
  • 117
Yoga
  • 293
  • 3
  • 8
  • 23
  • Well, you push `$("#valueList").text()`, which is the String representation of the Array. Have you tried just pushing `$("#valueList")` instead? – Carl Shiles Jun 19 '17 at 13:39
  • Possible duplicate of [Populating JavaScript Array from JSP List](https://stackoverflow.com/questions/3040645/populating-javascript-array-from-jsp-list) – Jozef Chocholacek Jun 19 '17 at 13:50

1 Answers1

0

Try this

//Assumming your value is like this or you should assign json string in valueList span 
// Other wise you can use jstl to iterate the array
<span id="valueList" style="display: none;">[1,2,3,4,5,6]</span>

var ranges = [];
ranges = JSON.parse($("#valueList").text());
$.each(ranges , function(i, item) {
    console.log('val is '+ item) 
});
Krazy_Tech
  • 316
  • 3
  • 11