0

I have a route object. It contains arrayList of options. I want to know the size of options and store it in some variable. (This thing needs to be done in JSP). I then want to access this variable in jquery. How to do it?

JSP code :

<%!int loopSize = routes.get(0).getOptions().size(); %>
  <c:forEach var="route" items="${routes}" varStatus="loopCounter">
    <c:forEach var="option" items="${route.options}" 
               varStatus="loopCounter2">
    <---more code -->
    </c:forEach> 
  </c:forEach>

But it is saying routes undefined. I am using struts2 and action class. Routes is coming from the action class

5

1 Answer 1

2

One solution may be to store the length of array in hidden field and access it in jquery.

<input type="hidden" id="loopSize" name= "loopSize" value= "${fn:length(routes[0].options)}"/>

And using the id you can accesss the value in jquery

Sign up to request clarification or add additional context in comments.

1 Comment

This really isn't the best answer. Yes it will work, but why (1) assign a variable to hidden input so that you can (2) retrieve the value of the hidden input via JavaScript and (3) assign it to a JavaScript variable when you could simply assign it to the variable. See my answer below as well as this stackoverflow question and this stackoverflow question.

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.