Solved

alternate row color

Posted on 2006-11-28
6
376 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
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…

740 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