Solved

alternate row color

Posted on 2006-11-28
6
377 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 

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

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!

Question has a verified solution.

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

Suggested Solutions

Avoid defining the variables in the global scope; trying to define them in a local function scope. Because:   • Look-up is performed every time a variable is accessed.   • Variables are resolved backwards from most specific to least specific scope…
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-…
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…

731 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