Solved

alternate row color

Posted on 2006-11-28
6
375 Views
Last Modified: 2008-03-04
I would like to know if there s a script that would alternate the color of rows on a table using javascript.

i found a solution on EE that uses this function:
<script language="javascript">
var color1="#AAAAAA";
var color2="#555555";
var headercolor="#0000CC";
// get table rows
rows=document.getElementById("table0").rows;
// set first row color
rows[0].setAttribute("bgcolor",headercolor);
// loop through remaining rows
for (i=1;i<rows.length;i++){
     // if modulus=0, set color1, else modulus=1, set color2
     if (i%2==0)
          rows[i].setAttribute("bgcolor",color1);
     else
          rows[i].setAttribute("bgcolor",color2);
}
</script>

and it is placed right after the </html> closing tag. the problem is that it only works on Firefox not on IE.
I was wondering if someone could help me tweak the function so it works on both browsers, or if
another function could be shared taht does the same.

i don't want to use CSS because i have several rows and they keep increasing.

for your help thank you in advance.

regards,

itortu.
0
Comment
Question by:itortu
  • 3
  • 3
6 Comments
 

Author Comment

by:itortu
ID: 18031399
i forgot to mention that i also found other scripts that use body onload but i am already using another script on body onload and i don't know how to mix the two of them.

this is my body tag:

<body leftmargin="1" marginheight="0" marginwidth="1" topmargin="0" onLoad='if (domok) initTable("table0")'>
0
 
LVL 63

Accepted Solution

by:
Zvonko earned 150 total points
ID: 18031669
Either try the uppercase Color attribute:
  rows[i].setAttribute("bgColor",color1);

Or use the style attribute:
  rows[i].style.backgoundColor = color1;

Or use CSS classes.
Like this:
  rows[i].className = "rowSytle1";





0
 
LVL 63

Expert Comment

by:Zvonko
ID: 18031682
Oh, and putting the script outside of html is not good. Put it simply after </table> that you want to access.

0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 

Author Comment

by:itortu
ID: 18031940
let me try your ideas.

thank you!
0
 

Author Comment

by:itortu
ID: 18032105
changing it to
rows[i].setAttribute("bgColor",color1);

fixed it.

thank you Zvonko!
0
 
LVL 63

Expert Comment

by:Zvonko
ID: 18032185
You are welcome.
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Allow tab to be displayed without being clicked and active color border changed 2 32
how can i make 2 columns? 4 41
html Uncheck Checkbox 2 25
html border input line 7 15
The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question