Question

I have an issue with a parameter that I'm trying to pass into a Javascript function where the parameter gets cut off.

In my Servlet, I have set a parameter request.setAttribute("questions", service.getQuestions("123"))

It sets a list of questions with each question containing several values;

I loop through them with a JSTL loop <c:forEach var="data" items="${questions}">...</c:forEach> which I can then access the values as so ${data.question}, ${data.options} etc.

console.log(${data.question}) returns a value of the form 123,45,35|43,94,73|23,91,34 which is as expected.

But when I try to pass this ${data.question} into a javascript function such as <script>MyFunction(${data.question})</script>, it only receives 123.

MyFunction(data) {
    console.log(data); //Only shows 123
    //Split the string into arrays for processing
}
Was it helpful?

Solution

You receive first element becouse your function expect one parameter, and your value 123,45,35|43,94,73|23,91,34 is split by comma so it looks for function like diferent parametrs. Use arguments property insted or pass all value as string in '' like this

<script>MyFunction('${data.question}')</script>

Sorry for my english.. still working on it

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top