Use javascript variables in JSP code

70 views Asked by At

I want to use javascript variables in JSP code . I know there is issueof Client side and server side variables , but still any way to do so without interacting with server or servlet??

code:

<form>
    <select name="select" id="form_events">
        <%
            for (int i = 1; i <= 5; i = i + 1) {
        %>
        <option value=<%=i%>>
            <%=i%></option>
        <%
            }
        %>

    </select>

</form>

Can any one help me in assigning value to variable "val" ??

1

There are 1 answers

1
meskobalazs On

You can't, JSP is on the server side, JS is on the client size. Also, you should not use scriptlets, it makes your code similar to a bowl of spaghetti.

If you only need to generate JavaScript code you can do that e.g. by using this syntax:

var variable = '<%=myJspVariable.toString()%>';

This renders the myJspVariable as a string, and it can be used in your JavaScript code.