?
Solved

alternate row color

Posted on 2006-11-28
6
Medium Priority
?
379 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 600 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
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…
Suggested Courses

765 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