Solved

How to assign JSP Variable to JavaScript

Posted on 2007-11-16
9
5,977 Views
Last Modified: 2012-08-13
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
0
Comment
Question by:knaren_1975
  • 2
  • 2
  • 2
  • +2
9 Comments
 
LVL 18

Expert Comment

by:Morcalavin
ID: 20299014
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

0
 
LVL 18

Expert Comment

by:Morcalavin
ID: 20299022
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

0
 
LVL 17

Expert Comment

by:contactkarthi
ID: 20299048

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

Open in new window

0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

Author Comment

by:knaren_1975
ID: 20299156
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
0
 
LVL 17

Expert Comment

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

Open in new window

0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 20299223
function sortResults(sortOption) {
  var proId= '<%= (request.getAttribute("id")!=null)?(String)request.getAttribute("id"):"" %>';
}
0
 
LVL 11

Accepted Solution

by:
Manish earned 500 total points
ID: 20317713
Just some modification in given code.
function sortResults(sortOption) {

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

Author Closing Comment

by:knaren_1975
ID: 31409571
THx
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 20712577
Did you try mine?
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question