/    Sign up×
Community /Pin to ProfileBookmark

CSS Issue with Table Row Color

I am trying to use javascript to apply CSS classes to an HTML table I have on a website. Every other row should be the same color + there should be a hover color on mouseover. I have tested the table outside the site & I don’t think this is a javascript issue, I believe it is something with CSS. I have been looking at this for several hours, but perhaps another pair of eyes can alert me to the issue? Thanks! confused:

HTML

[CODE]<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml”>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″ />
<title>Website</title>
<link rel=”stylesheet” type=”text/css” href=”css/styles1.css” media=”screen” />
<script type=”text/javascript” src=”scripts/links.js”></script>
</head>

<body>
<div id=”wrapper”>
<div id=”masthead”>
<h1>Header 1</h1>
<h2>Header 2</h2>
</div>
<ul id=”navbar”>
<li><a href=”index.html”>Home</a></li>
<li><a href=”faq.html”>FAQ</a></li>
<li><a href=”links.html”>Links</a></li>
<li><a href=”gallery.html”>Photos</a></li>
<li><a href=”contact.html”>Contact</a></li>
</ul>
<div id=”content”>

<h1>Resources</h1>
<table id=”myTable”>
<tr>
<th>Number</th>
<th>Link Address</th>
</tr>
<tr>
<td>1</td>
<td><a href=”http://www.google.com” rel=”nofollow”>Link 1</a></td>
</tr>
<tr>
<td>2</td>
<td><a href=”http://www.google.com” rel=”nofollow”>Link 2</a></td>
</tr>
<tr>
<td>3</td>
<td><a href=”http://www.google.com” rel=”nofollow”>Link 3</a></td>
</tr>
<tr>
<td>4</td>
<td><a href=”http://www.google.com” rel=”nofollow”>LInk 4</a></td>
</tr>
</table>
<p>&nbsp;</p>
</div>
</div>
<div id=”bottom”></div>
</body>
</html>
[/CODE]

Javascript:

[CODE]

window.onload = function(){stripeyTables();
}
//ADD CLASS FUNCTION TO APPEND CSS VALUES TO ANY OTHER CSS
function addClass(element,value) {
if (!element.className) {
element.className = value;
} else {
newClassName = element.className;
newClassName+= ” “;
newClassName+= value;
element.className = newClassName; }
}
//Alternate Table Rows & Hover Color Function
function stripeyTables() {
if (!document.getElementsByTagName) return false;
var table = document.getElementById(“myTable”);
var odd = false;
var rows = table.getElementsByTagName(“tr”);
for (var j=1; j<rows.length; j++) {
if (odd == true) {
addClass(rows[j],”first”);
odd = false;
} else {
addClass(rows[j],”second”);
odd = true;
}
rows[j].oldClassName = rows[j].className
rows[j].onmouseover = function() {
addClass(this,”hover”);
}
rows[j].onmouseout = function() {
this.className = this.oldClassName
}
}
}[/CODE]

CSS:

[CODE]
#myTable {
border: 1px solid #333;
border-collapse: collapse;
}
#myTable th, #myTable td {
padding: 5px;}

.first {
background: #78AB46;
}
.second {
background: #8E2323;
}
.hover {
background: #CFCD11;

}
th, td {
padding: 5px;
}[/CODE]

to post a comment
CSS

2 Comments(s)

Copy linkTweet thisAlerts:
@FangDec 06.2010 — Works OK ?
Copy linkTweet thisAlerts:
@mtnhcmgrlauthorDec 06.2010 — Well, that is the weirdest thing. I made 3 new files, copied & pasted them and low and behold, that worked. I wonder what in the world was wrong w/ my original files?

:eek:

Thanks for your help again!
×

Success!

Help @mtnhcmgrl spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.24,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...