Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass a JSP variable as parameter to javascript function

Tags:

jsp

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

like image 635
Naveen Avatar asked Apr 05 '12 17:04

Naveen


2 Answers

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>
like image 60
Ramesh Kotha Avatar answered Sep 30 '22 13:09

Ramesh Kotha


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.

like image 21
Chayan Ghosh Avatar answered Sep 30 '22 12:09

Chayan Ghosh