j-money
j-money

Reputation: 529

Pass jsp variable to javascript

I am curious as to how I should be passing a variable from my .jsp page to my JavaScript file. Currently I have the following...

<c:forEach items="${stats}" var="s" >
        <script type="text/javascript">
            var status = "hello";
        </script>
            <div class='memory' id='memory'>
                <p id='text'>Memory<p>
            </div>
            <div class='memory1' id='memory1'>
                <p id='text'> Memory 2</p>
            </div>  
             <script  src="js/temp.js" type="text/javascript"></script>
        </c:forEach>

This works just fine for alert(status); However I am wondering how I should go about passing the field of {$s.status} to the javascript file as changing

<script type="text/javascript">
    var status = {$s.status};
</script>

does not work

Upvotes: 0

Views: 1726

Answers (3)

Mychelrenno
Mychelrenno

Reputation: 1

Try this:

var test = <%=request.getParameter("test")%>;

Upvotes: 0

Akash Srivastava
Akash Srivastava

Reputation: 1

The best way to do it is to use something like:

var status = '${s.status}';

Upvotes: 0

Quang Dat Pham
Quang Dat Pham

Reputation: 188

Another way to do it:

<input type="hidden" value="${s.status}" id="status">
<script>
   var status = document.getElementById('status').value;
</script>

Upvotes: 3

Related Questions