0

Hi i want to use jsp variable value in javascript How can i use

Here is my code.

Demo.jsp

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <%
        String plane="A";
        %>
        <script>
     var planB=<%=plane%>;
     c
     if(planB==B){
     document.write("shakti");
     }else{
     document.write("sharma");
     }
     </script>
</body>
</html>

How can i get my desired output

1
  • 1
    try var planB='<%=plane%>'; Commented May 10, 2014 at 10:48

1 Answer 1

1

Change your script block with the following:

<script>
    var planB = '<%=plane%>';
    if(planB === 'B'){
        document.write("shakti");
    } else{
        document.write("sharma");
    }
</script>

Changes:

  • Your jsp variable should be inside single (or double) quotes, otherwise you'll get an error: "A is undefined"
  • You have a character 'c' in the script-block, that'll also throw a javascript error.
  • I've changed '==' to '===' (in the if-check), so that you not only check the values, but also the types.
  • You have to place 'B' (in the if-check) between single (or double) quotes, otherwise you'll get an error: "B is undefined". You want to compare the variable to a string, not a variable ==> place inside single (or double) quotes.
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.