Link to home
Start Free TrialLog in
Avatar of knaren_1975
knaren_1975

asked on

How to assign JSP Variable to JavaScript

Hi
i have a function like below
    function sortResults(sortOption) {
    <% if(request.getAttribute("id")!=null){
             String pId = (String)request.getAttribute("id");
       var proId= '<%=pId%>';
      }%>
    }
i am not able to assign the pid value to proId it is showing null..where as i am getting the id value
please let me whethe i am properly assign this jsp value in java script variable
Avatar of Morcalavin
Morcalavin
Flag of United States of America image

I don't know much about jsp, but I would think this would be proper:

 function sortResults(sortOption) {
    <% if(request.getAttribute("id")!=null){
             String pId = (String)request.getAttribute("id");
       }
    %>
       var proId= '<%=pId%>';
    }

Open in new window

Or this, depending on which part is javascript and which is jsp
function sortResults(sortOption) {
    if(request.getAttribute("id")!=null){
    <%
             String pId = (String)request.getAttribute("id");
    %>
       var proId= '<%=pId%>';
    }
    }

Open in new window

Avatar of contactkarthi

function sortResults(sortOption) {
    <% if(request.getAttribute("id")!=null){
             String pId = (String)request.getAttribute("id"); 
%>
       var proId= '<%=pId%>';
  <%
    }%>
    }

Open in new window

Avatar of knaren_1975
knaren_1975

ASKER

it's not working...

i am getting the request.getAttribute("id") value but i am not able to assign this value into javascript varible

i but some debug statement then i can see the value coming into this.
when i add  the code var proId= '<%=pId%>'; my jsp page itself is not opening just saying  null in this Jsp

Regards
Naren
did u try
function sortResults(sortOption) {
    <% if(request.getAttribute("id")!=null){
             String pId = (String)request.getAttribute("id"); 
%>
       var proId= '<%=pId%>';
  <%
    }%>
    }

Open in new window

function sortResults(sortOption) {
  var proId= '<%= (request.getAttribute("id")!=null)?(String)request.getAttribute("id"):"" %>';
}
ASKER CERTIFIED SOLUTION
Avatar of Manish
Manish
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
THx
Did you try mine?