Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get a index value from foreach loop in jstl

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()".

like image 829
Java Questions Avatar asked Sep 16 '13 10:09

Java Questions


1 Answers

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> 
like image 90
newuser Avatar answered Sep 18 '22 21:09

newuser