Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Applying alternate colours to table rows 1

Status
Not open for further replies.

SkippyK

Programmer
Oct 10, 2002
9
GB
I have a table embedded within my html that is being dynamically generated by a script. In order to highlight the contents of each individual record being listed, I want to be able to alternate the background colour of each row.

How on earth would I achieve this?

TIA

 
Use CSS

then for every iteration of the loop building the table toggle a Boolean variable and change the style for the <tr> for the True or False value.



Chris.






Indifference will be the downfall of mankind, but who cares?
 
something like...

var bgSwitch = true;
var bg = (bgSwitch) ? &quot;#00ffff&quot;:&quot;#ffff00&quot;;

for (x=0;x<numRows;x++){
document.write ('<tr bgcolor=&quot;' + bg + '&quot;><td>value</td></tr>')
bgSwitch = !bgSwitch
}

...should do the trick
 
Thank you Mr 3,

I've adapted what you've suggested. My pages are not currently using .CSS, so you're quick fix is more appropriate at this time.

Thanks for the suggestions. Hope the putting improves.

 
Chris

a star for your idea, and i didnt even have to ask
[pipe]
 
As opposed to taking the space for the extra boolean, it might be cleaner if your test is a mod 2...

for (x=0;x<numRows;x++){
bg = (x%2 == 0) ? &quot;#00ffff&quot;:&quot;#ffff00&quot;;
document.write ('<tr bgcolor=&quot;' + bg + '&quot;><td>value</td></tr>');
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top