Win a copy of The Way of the Web Tester: A Beginner's Guide to Automating Tests this week in the Testing forum!

# A different value on odd an even number in iteration

Sverre Moe
Ranch Hand
Posts: 110
I am iterating over a table, producing a new row at each iteration. What I need is to use a different class="" for the even and odd numbers.

The first row will start with a light row, while the next will have a dark row. Light row will occour in row/iteration 1,3,5,7,9,11,13, etc. and dark will occour in 2,4,6,8,10, etc.

How can I perform this without doing some mathematical operations in the iteration number?

Ben Souther
Sheriff
Posts: 13411
You could do it by toggling a flag with each iteration but that would be a lot more work than using the modulus operator --which is a one liner.

Why don't you want do to do this with math?

Sverre Moe
Ranch Hand
Posts: 110
Originally posted by Ben Souther:
You could do it by toggling a flag with each iteration but that would be a lot more work than using the modulus operator --which is a one liner.

Why don't you want do to do this with math?

I thought it would be more code, but that was very elegant and better than my 5 line c:choose, c:when code.

I was unaware that I could have boolean logic within a html tag like that without c:if, c:choose/c:when.
[ February 19, 2008: Message edited by: Sverre Moe ]

Ben Souther
Sheriff
Posts: 13411
Originally posted by Sverre Moe:

I was unaware that I could have boolean logic within a html tag like that without c:if, c:choose/c:when.

Just so it's clear, that logic isn't being done by the HTML interpreter (which is on the client's machine). The \${} tag is a JSP Expression Language (EL) expression and is performed on the server. If you look at the generated HTML source, you'll only see the resulting class names, not the expression itself.

There is a copy of the JSP spec in my signature.
It shouldn't take more than 20 minutes to skim the whole thing.
This will give you a good idea of what JSP 2.0 has to offer.
[ February 19, 2008: Message edited by: Ben Souther ]