-1

I am using <c:set> tag to set the key value to key1.

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

I need to pass this 'Key1' as a parameter of a function. for eg- onclick=test(--here I need to pass 'Key1'---).

How do I pass ?

Thanks.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
ranju
  • 13
  • 2
  • 5

1 Answers1

0

JSP is a HTML code generator. JS is part of HTML.

You actually don't "pass" variables from JSP/JSTL to HTML/JS. You basically let JSP "print" HTML/JS code the way you want, including JS variables. You only need to make sure that you write JSP code in such way that it generates syntactically valid HTML/JS code output.

Thus, so:

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

Note the importance of singlequotes if the variable represents a character sequence, e.g. from a Java String variable. You of course want it to end up in generated HTML output as below (you can verify via rightclick, View Source in webbrowser):

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

and thus not like so:

onclick="test(value of variable Key1)"

It would however work fine without singlequotes if the variable actually represents a number:

onclick="test(${Key1})"

which should then end up in HTML output like this:

onclick="test(42)"
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • I have tried this onclick="test('${Key1}')". But When I print the value of key1 inside the function test not getting the value , instead of it is getting ${Key1} as value; – ranju Jul 15 '15 at 07:09
  • It's supported since JSP 2.0 (released more than a decade ago). Apparently your environment is jurassic or misconfigured. If jurassic, make sure you upgrade to latest as soon as possible. If misconfigured, or you followed some random low quality blog in Internet instead of Oracle tutorials/documentation, or you have no idea what you're doing, then this perhaps answers your EL problem: http://stackoverflow.com/questions/30080810/el-expressions-not-evaluated-anymore-after-including-jstl-in-webapp/ – BalusC Jul 15 '15 at 07:11