Struggling with what is probably simple code to append double quotes to each element either in the c:set or in the ArrayList or in the JavaScript variable.

After a c:set (which contains mother, baby etc) I populate an ArrayList (which then contains [mother, baby, child, staff] etc) and then 'get it' into JavaScript.

The final output is [mother, child, staff] etc but I need it to be ["mother", "baby", "child", "staff"] etc

After a sql:select - 
<c:set var="type" value="${row.Type}"  />

Add to an ArrayList -
       types.add(pageContext.getAttribute("type"));

Get into javascript (after the jstl forEach loop completes)-

var typesArray = [<% for (int i = 0; i < types.size(); i++) { %>"<%=types.get(i)%>"<%= i + 1 < types.size() ? ",":"" %><% } %>];    

Can't add double quotes to the c:set because it then escapes / fails -

<c:set var="type" value=""${row.Type}""  />

I don't really want to use StringBuffer because that affects the JavaScript get iterator which took me days to get working.

Assume old Java / old Javascript

Thoughts appreciated. Regards. Ralph

2 Answers

0
Srinivasan Sekar On Best Solutions

Use single / double quote wrapping ,

<c:set var="type" value="'${row.Type}'"  /> 

or

<c:set var="type" value='"${row.Type}"'  />

Similarly ,

var typesArray = [<% for (int i = 0; i < types.size(); i++) { %>"'<%=types.get(i)%>'"<%= i + 1 < types.size() ? ",":"" %><% } %>]; 

or

var typesArray = [<% for (int i = 0; i < types.size(); i++) { %>'"<%=types.get(i)%>"' <%= i + 1 < types.size() ? ",":"" %><% } %>];
0
Guilherme Abacherli On

Did you try something like:

<c:set var="type" value="\"${row.Type}\""  />