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.

2
  • Well, you push $("#valueList").text(), which is the String representation of the Array. Have you tried just pushing $("#valueList") instead? Commented Jun 19, 2017 at 13:39
  • Possible duplicate of Populating JavaScript Array from JSP List Commented Jun 19, 2017 at 13:50

1 Answer 1

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) 
});
Sign up to request clarification or add additional context in comments.

Comments

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.