I have the following code sample which was written in the JSP. But it is not working. I have tried that code snippet from the following links. But it didn't help me to solve the problem.
Pass a JSP variable as parameter to javascript function
Passing Jsp variable to javascript as a function parameter
< c:forEach items="${items}" var="item">
<div onclick="myOnClick('<%=item%>')">${item.title}</div>
< /c>
function myOnClick(item){
console.log(JSON.stringify(item));
}
Actually I am getting an error in the eclipse that "item cannot be resolved to a variable" for the JSP code.
I tried the below one code
<div onclick="myOnClick(${item})">${item.title}</div>
It was throwing "Syntax error:illegal character" error.
Need help to fix this issue. Thank you.
<%=item%>instead of${item}?? Anyway, what you insert is a string (you even quoted it with'), which is retrieved by callingitem.toString(), so what is it you expect thatstringify()call to do? I mean, it's already a string, and very likely not a JSON string, unless you implementedtoString()to return JSON.${item}.${item}when${items}is working fine on the line above?? I find that very hard to believe. Or are you now talking about the web browser, not the JSP (because you forgot the add the'quotes)??