javascript - How do I pass c:set variable as a java script function parameter -


i using <c:set> tag set key value key1.

<c:when>     <c:set var="key1" value="${key}" />     <a href="some url" onclick="test(--here need pass  'key1'---)">click here</a> </c:when> 

i need pass 'key1' parameter of function. eg- onclick=test(--here need pass 'key1'---).

how pass ?

thanks.

jsp html code generator. js part of html.

you don't "pass" variables jsp/jstl html/js. let jsp "print" html/js code way want, including js variables. need make sure write jsp code in such way generates syntactically valid html/js code output.

thus, so:

onclick="test('${key1}')" 

note importance of singlequotes if variable represents character sequence, e.g. java string variable. of course want end in generated html output below (you can verify via rightclick, view source in webbrowser):

onclick="test('value of variable key1')" 

and not so:

onclick="test(value of variable key1)" 

it work fine without singlequotes if variable represents number:

onclick="test(${key1})" 

which should end in html output this:

onclick="test(42)" 

Comments