How to get a index value from foreach loop in jstl

263,499

Solution 1

use varStatus to get the index c:forEach varStatus properties

<c:forEach var="categoryName" items="${categoriesList}" varStatus="loop">
    <li><a onclick="getCategoryIndex(${loop.index})" href="#">${categoryName}</a></li>
</c:forEach>

Solution 2

I faced similar problems. After research I understand we have some more options with the varStatus="loop". It can either use a zero based index or one based index:

  • The ${loop.index} uses a 0 base index
  • The ${loop.count} uses a 1 base index

For Example :

<c:forEach var="currentImage" items="${cityBannerImages}" varStatus="loop">
<picture>
   <source srcset="${currentImage}" media="(min-width: 1000px)"></source>
   <source srcset="${cityMobileImages[loop.count]}" media="(min-width:600px)"></source>
   <img srcset="${cityMobileImages[loop.count]}" alt=""></img>
</picture>
</c:forEach>

For more Info please check this link.

Solution 3

You can use the varStatus attribute like this:-

<c:forEach var="categoryName" items="${categoriesList}" varStatus="myIndex">

myIndex.index will give you the index. Here myIndex is a LoopTagStatus object.

Hence, you can send that to your javascript method like this:-

<a onclick="getCategoryIndex(${myIndex.index})" href="#">${categoryName}</a>

Solution 4

This works for me:

<c:forEach var="i" begin="1970" end="2000">
    <option value="${2000-(i-1970)}">${2000-(i-1970)} 
     </option>
</c:forEach>
Share:
263,499
Java Questions
Author by

Java Questions

Updated on July 19, 2021

Comments

  • Java Questions
    Java Questions almost 3 years

    I have a value set in the request object like the following,

    String[] categoriesList=null;
    categoriesList = engine.getCategoryNamesArray();
    request.setAttribute("categoriesList", categoriesList );
    

    and this is how I iterate in jsp page

    <% if(request.getAttribute("categoriesList") != null) { %>
    <c:forEach var="categoryName" items="${categoriesList}">
       <li><a onclick="getCategoryIndex()" href="#">${categoryName}</a></li>
    </c:forEach>
    <% }%>
    

    How do I get index of each element and pass it to JavaScript function onclick="getCategoryIndex()".

  • Java Questions
    Java Questions over 10 years
    I get this Uncaught ReferenceError: myIndex is not defined and +1 for your effort
  • Java Questions
    Java Questions over 10 years
    I get this Uncaught ReferenceError: loop is not defined ` and +1 for your effort
  • Java Questions
    Java Questions over 10 years
    onClick of the element displayed
  • newuser
    newuser over 10 years
    Because the varStatus value contains value upto the loop iteration. After that the value is not a valid one. so only you got the exception. For which purpose you need the index
  • Java Questions
    Java Questions over 10 years
    I need to know the index[location] of each element comes in the String Array.
  • Java Questions
    Java Questions over 10 years
    it works :) if I do this way onclick="getCategoryIndex(${loop.index})" ...thanks for the help
  • Java Questions
    Java Questions over 10 years
    if I want to store this loop value in to a variable and pass the same to javascript method than how do I go about ?
  • Java Questions
    Java Questions over 10 years
    I sued this way <input type="hidden" id="categoryIndex" name="categoryIndex" value="${category.index}"/> but it always gives me 0 when do this var categoryIndex = $('#categoryIndex').val();
  • Java Questions
    Java Questions over 10 years
    created new question http://stackoverflow.com/questions/18827153/jstl-foreach-loo‌​p-setting-values-to-‌​a-hidden-variable-re‌​turns-zero-always
  • newuser
    newuser over 10 years
    Sorry now only i saw this
  • Daniel Carpio Contreras
    Daniel Carpio Contreras about 6 years
    I think the answer is like this: <c:forEach items="${pageScope.names}" var="currentName" varStatus="status"> Family member #<c:out value="${status.count}" /> is <c:out value="${currentName}" /> <br /> </c:forEach> Check this link