473,396 Members | 1,990 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Alternating Table Background Colour

Hi

I want rows of a table to appear in alternating background colours. Having looked around the web I can't find any solution apart from waiting for the next version of CSS, so I tried building one myself, but I need some help to finish it.

I use the getAttribute('rowIndex') to return the position of a row within the table and apply modulo 2 to return a 0 or 1 indicating an even or odd row number, from which I apply the alternating background colours (in this case garish red and yellow, just in case any of you were feeling sleepy).

My problem is how I can apply these colours to the table. The nearest I have been able to get is onclick - but of course the background colour only gets applied when the user clicks on the table. Obviously what I really need is an "onload" event but I can't find anything like this which works with the TR element.

Does anyone have any suggestions? Or maybe I am going in completely the wrong direction.

Yours

Alistair Birch
Albany, Western Australia

Best attempt so far (using onclick, so you'll have to click on each row to see the colour, but it works otherwise):

-----------------------don't cut here you'll ruin your monitor------------------------------------------------
<table border="1">
<tr onclick="style.backgroundColor=((this.getAttribute ('rowIndex')%2)==0?'red':'yellow')">
<td>1</td>
</tr>
<tr onclick="style.backgroundColor=((this.getAttribute ('rowIndex')%2)==0?'red':'yellow')">
<td>2</td>
</tr>
<tr onclick="style.backgroundColor=((this.getAttribute ('rowIndex')%2)==0?'red':'yellow')">
<td>3</td>
</tr>
<tr onclick="style.backgroundColor=((this.getAttribute ('rowIndex')%2)==0?'red':'yellow')">
<td>4</td>
</tr>
<tr onclick="style.backgroundColor=((this.getAttribute ('rowIndex')%2)==0?'red':'yellow')">
<td>5</td>
</tr>
</table>

Jul 20 '05 #1
1 4536
Oz
Generally to keep my script simple I only apply the onload event to the document as a whole and not individual elements. Here is a simple document onload implementation that accomplishes your goal:
<html>
<head>
<script language="javascript">
function init(){
var table = document.getElementById("table");
var rows = table.tBodies[0].childNodes;
for (var i=0;i<rows.length;i++) {
rows[i].style.backgroundColor = (i%2 == 0) ? "red" : "yellow";
}
}
</script>
</head>
<body onload="init()">
<table border="1" id="table">
<tr>
<td>1</td>
</tr>
<tr>
<td>2</td>
</tr>
<tr>
<td>3</td>
</tr>
<tr>
<td>4</td>
</tr>
<tr>
<td>5</td>
</tr>
</table>
</body>
</html>


"Alistair Birch" <alistairb at fpc wa gov au> wrote in message news:3f********@quokka.wn.com.au...
Hi

I want rows of a table to appear in alternating background colours. Having looked around the web I can't find any solution apart from waiting for the next version of CSS, so I tried building one myself, but I need some help to finish it.

I use the getAttribute('rowIndex') to return the position of a row within the table and apply modulo 2 to return a 0 or 1 indicating an even or odd row number, from which I apply the alternating background colours (in this case garish red and yellow, just in case any of you were feeling sleepy).

My problem is how I can apply these colours to the table. The nearest I have been able to get is onclick - but of course the background colour only gets applied when the user clicks on the table. Obviously what I really need is an "onload" event but I can't find anything like this which works with the TR element.

Does anyone have any suggestions? Or maybe I am going in completely the wrong direction.

Yours

Alistair Birch
Albany, Western Australia

Best attempt so far (using onclick, so you'll have to click on each row to see the colour, but it works otherwise):

-----------------------don't cut here you'll ruin your monitor------------------------------------------------
<table border="1">
<tr onclick="style.backgroundColor=((this.getAttribute ('rowIndex')%2)==0?'red':'yellow')">
<td>1</td>
</tr>
<tr onclick="style.backgroundColor=((this.getAttribute ('rowIndex')%2)==0?'red':'yellow')">
<td>2</td>
</tr>
<tr onclick="style.backgroundColor=((this.getAttribute ('rowIndex')%2)==0?'red':'yellow')">
<td>3</td>
</tr>
<tr onclick="style.backgroundColor=((this.getAttribute ('rowIndex')%2)==0?'red':'yellow')">
<td>4</td>
</tr>
<tr onclick="style.backgroundColor=((this.getAttribute ('rowIndex')%2)==0?'red':'yellow')">
<td>5</td>
</tr>
</table>

Jul 20 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: bettyann | last post by:
i am using templates via HTML_Template_IT to create a table from the results of a mysql query. i wanted to change the background image of alternating rows in the table. i thought i could do this...
47
by: Matt Kruse | last post by:
http://www.mattkruse.com/temp/css_expressions.html One of the standard CSS questions is "how can I shade every other table row a different color with CSS?" The answers are usually 1) you can't...
3
by: blackdevil1979 | last post by:
Hi, Just curious, can the crystal report show (during report view and in the printed report) different background colour for alternate rows? For example: 1 row background colour =silver 2...
1
by: Steve Bottoms | last post by:
Hi, all! How does one prevent table cell background colors from alternating between grey and white? I've dropped a basic table control on an ASP.Net page (using VB.Net codebehind), and...
10
by: Bob Bedford | last post by:
I've a table in wich I've this CSS: ..oddrow{background-color:#FFFFFF} ..evenrow{background-color:#CCCCCC} The oddrows are white and the even are grey. BUT ! when I do print the table,...
117
by: phil-news-nospam | last post by:
Is there really any advantage to using DIV elements with float style properies, vs. the old method of TABLE and TR and TD? I'm finding that by using DIV, it still involves the same number of...
4
by: crab.dae | last post by:
Not sure if any one can help but I'm loading data into a table that I've like to alternate the background color of the rows (White then pink). Can someone tell me what I need to add to do this?...
14
by: Tristan Miller | last post by:
Greetings. Is it possible with CSS to have an HTML <tablesuch that even-numbered <trrows have one background colour, and odd-numbered <trrows have another background colour? This makes wide...
3
Frinavale
by: Frinavale | last post by:
Hi there, I'm using JavaScript to highlight a row or column in a table. I have created a CSS class that changes the background colour to a light blue and I apply this class to the cells in the...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.