yungCoder
yungCoder

Reputation: 11

How do i get my values from my .jsp into my javascript?

This is my first question on stackoverflow. So I'm doing a university project for web developement. I have data (severel integers) on my PostgreSQL server. For example 4, 5 and 7. I let my Servlet (java) post it on my HTML (jsp) via expression language.

<ul> 
    <c:forEach var="i" items="${ arrayM }"> //arrayM is a ArrayList of a object from my server

            <li id="values">${ i.getNumber() }</li>
    </c:forEach>
</ul> 

I tried reading the values with document.getElementById("values").innerHTML;But the type of that object is a String. I need the values in an array or something. So I can put it into another method:

data: [
    4, 5, 7

     ],

It is a part of my chart.js method. I'm pretty new to JavaScript.

I tried this:

var x = document.getElementById("values").innerHTML;
var array = [x];

and

parseInt(array.toString()),

Only gives me the first value.

Upvotes: 1

Views: 54

Answers (1)

Makary
Makary

Reputation: 66

Id of element is unique in HTML, so first of all you should change <li id="values">...</li> to <li class="value">...</li> because you can't have multiple elements with the same id.

Next instead of document.getElementById("values").innerHTML; which only return you single value, use document.querySelectorAll(".value"); to get all items of "value" class as NodeList of Elements.

From there you can find many ways to use your data, for example if you want to do make an array you can use something like this

const values = document.querySelectorAll(".value");
let valuesArr = [];
values.forEach(function(value){
    valuesArr.push(parseInt(value.innerHTML))
})

Upvotes: 1

Related Questions