Solved

alternate row color

Posted on 2006-11-28
6
372 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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-…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…

758 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now