sherry
sherry

Reputation: 1859

Creating rows of a table in a for loop in jsp

In a jsp I have a table whose rows I am creating in a loop like this,

<table>
<tr>
<th>Item</th>
<th>Quantity</th>
<th>Price</th>
<th>Total</th>
<th>Actions</th>
</tr>
<tr>
<%
String[] cartItems = (String[]) request.getSession().getAttribute("cartList");
for (int i = 0; i < cartItems.length; i++) {
%>
   <td>
      <input type="text" id="itemPrice" maxlength="5" size="5" style="border:none;" value="$<%= cartItem[3]%>" />
   </td>
<% } %>
</tr>
</table>

Suppose 5 such rows are added, every row will have the id=itemPrice, but I want the rows to have:

id=itemPrice1
id=itemPrice2.. and so on..

How do I do this? Please help..

Upvotes: 4

Views: 24103

Answers (2)

darioo
darioo

Reputation: 47183

I'd say this should work (not tested):

Replace id="itemPrice" with id="itemPrice${i+1}"

This way, you're inserting value of i inside of your html.

If this doesn't work, try id="itemPrice<%=i+1%>".

Upvotes: 0

elvenbyte
elvenbyte

Reputation: 776

<input type="text" id="itemPrice<%=i%>" maxlength="5" size="5" style="border:none;" value="$<%= cartItem[3]%>" />

Note what happened with the "id". It's just the counter in the for sentence.

Upvotes: 5

Related Questions