Pass a JSP variable as parameter to javascript function

Go To StackoverFlow.com

8

I have a function defined inside the script tags of head.(in a JSP) I want to declare a string variable in the JSP and pass it as a parameter to this function

<%  String uname ="multiple"; %>
<form action="ExampleServlet" method="post" onclick="pagetype(${uname});"><br>
    <input type="submit" name="Log in" value="Login" />
</form>

But this doesn't work. need Help

2012-04-05 17:59
by Naveen


13

You have to use like this

<% String uname ="multiple"; %>
<form action="ExampleServlet" method="post" onclick="pagetype('<%=uname%>');"><br>
    <input type="submit" name="Log in" value="Login" />
</form>
2012-04-05 18:03
by Ramesh Kotha
Thanks Ramesh K

Its Workin - Naveen 2012-04-05 18:09

Yes @AdrienBe, i do agree, but for the OP it is the answer - Ramesh Kotha 2014-05-23 19:22


0

If you want to avoid scirplet, you can use expression language by putting it between single quotes.

onclick="pagetype('${uname}')";

without the quotes, it tries to look for a variable with name same as the value of uname.

PS: Used the chrome/firefox dev-tools debugging to find out what's going wrong.

2018-09-25 16:45
by Chayan Ghosh
Ads