Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Quick Javascript question...

Quick Javascript question...
Thread Tools
Mac Elite
Join Date: Apr 2003
Location: SoCal
Status: Offline
Reply With Quote
Mar 13, 2004, 04:45 PM
 
Okay, I know absolutely zip about JS (I'll learn it someday, I promise ), so you'll have to help me out with something that I imagine is stupidly simple to accomplish. The following is someone's compressed version of the Javascript used in ALA's Zebra Tables article:

Code:
onload = function() { stripe ('playlist') }; function stripe(id) { var table = document.getElementById(id); if (! table) { return; } var trs = table.getElementsByTagName("tr"); for (var i = 0; i < trs.length; i += 2) { trs[i].className += " even"; } }
Now, this was written to only affect tables with a specific ID (in this case, "playlist"). However, I want it to apply to every table in a document, regardless of whether or not it even has an ID.
(Last edited by Sage; Mar 13, 2004 at 04:55 PM. )
     
Forum Regular
Join Date: Feb 2004
Location: Manhattan
Status: Offline
Reply With Quote
Mar 13, 2004, 08:12 PM
 
you have to create a tr object and cycle through all the tr tags in the document. Anyway, here's an example of what I mean:

<script type="text/javascript">
function tableChange()

{

var rows = window.document.getElementsByTagName('tr');
for(var i = 0; i < rows.length; i++)
{
var trObj = rows.item(i);
if(i%2==0) {
trObj.style.backgroundColor = "blue";
}
else {
trObj.style.backgroundColor = "red";
}
} }
</script>
I haven't tested it, but it should be close.
That's just off the top of my head, so it might require some tweaking,.
(Last edited by DUNSEL; Mar 13, 2004 at 09:34 PM. )
I saw a woman with a sweatshirt that said "Guess", so I said, "Implants?"
     
Forum Regular
Join Date: Feb 2004
Location: Manhattan
Status: Offline
Reply With Quote
Mar 13, 2004, 10:16 PM
 
By the way, I should mention that with the function I posted above, no class designations are necessary anywhere in the table, not in the rows, or cells, unless of course you want them for some other purpose. Plugging this code into the head of the HTML document and putting onLoad="tableChange()" into the body tag, will alternate colors of any table on that document.
I saw a woman with a sweatshirt that said "Guess", so I said, "Implants?"
     
Forum Regular
Join Date: Feb 2004
Location: Manhattan
Status: Offline
Reply With Quote
Mar 13, 2004, 10:24 PM
 
Here's a real short version of the same function:

<script type="text/javascript">
function tableChange()

{

var rows = window.document.getElementsByTagName('tr');
for(var i = 0; i < rows.length; i++)
{
(i%2==0)? rows.item(i).style.backgroundColor = "lightblue" : rows.item(i).style.backgroundColor = "E0EFEE"

} }
</script>
I saw a woman with a sweatshirt that said "Guess", so I said, "Implants?"
     
Grizzled Veteran
Join Date: Nov 2003
Location: Hebburn, UK
Status: Offline
Reply With Quote
Mar 14, 2004, 05:06 AM
 
Wouldn't that mean that the scenario where you have one table alternating between blue then white then blue, but another table alternating between white then blue then white? Does that make sense?

If it's only looking at rows regardless of what table they are attached to, then the alternation of the colours will change, unless all the tables have an even number of rows...

Just change it to get the tables first, then their specific rows.
Just who are Britain? What do they? Who is them? And why?

Formerly Black Book
     
Mac Elite
Join Date: Oct 2000
Status: Offline
Reply With Quote
Mar 14, 2004, 08:22 AM
 
Originally posted by Black Book:
Wouldn't that mean that the scenario where you have one table alternating between blue then white then blue, but another table alternating between white then blue then white? Does that make sense?

If it's only looking at rows regardless of what table they are attached to, then the alternation of the colours will change, unless all the tables have an even number of rows...

Just change it to get the tables first, then their specific rows.
That's true... I also noticed that on the original alistapart example they had blue as the first color but it seems as if Apple always has white as the first color. I don't have much JavaScript experience so someone will probably be able to make this better/find a bug but I gave it a shot... independent table row color alternation with white as the first color:

Code:
onload = function() { stripe ('playlist') }; function stripe(id) { if (!(tables = document.getElementsByTagName("table"))) return; for (i=0; i < tables.length; i++) { trs = tables[i].getElementsByTagName("tr"); for (i2 = 1; i2 < trs.length; i2 += 2) trs[i2].className += " even"; } }
     
Forum Regular
Join Date: Feb 2004
Location: Manhattan
Status: Offline
Reply With Quote
Mar 14, 2004, 10:04 AM
 
I didn't realize that was important. Yeah, they might start with different colors, depending on how many rows in the tables, of course. If they're all of even numbered rows, then they'd all look the same. I just dashed off a quick function to alternate row colors. He didn't specify anything else.
I saw a woman with a sweatshirt that said "Guess", so I said, "Implants?"
     
Occasionally Useful
Join Date: Jun 2001
Location: Liverpool, UK
Status: Offline
Reply With Quote
Mar 14, 2004, 10:39 AM
 
Originally posted by Synotic:
That's true... I also noticed that on the original alistapart example they had blue as the first color but it seems as if Apple always has white as the first color.
which iTunes are you looking at? it can't be the same one i'm looking at:



(and that blue is #ECF3FE, by the way)
"Have sharp knives. Be creative. Cook to music" ~ maxelson
     
Forum Regular
Join Date: Feb 2004
Location: Manhattan
Status: Offline
Reply With Quote
Mar 14, 2004, 10:46 AM
 
"Dead in the Future"? LOL! Now that's a tune I'd be very interested in hearing.
I saw a woman with a sweatshirt that said "Guess", so I said, "Implants?"
     
Occasionally Useful
Join Date: Jun 2001
Location: Liverpool, UK
Status: Offline
Reply With Quote
Mar 14, 2004, 11:14 AM
 
Originally posted by DUNSEL:
"Dead in the Future"? LOL! Now that's a tune I'd be very interested in hearing.
http://www.ioaa.co.uk/
"Have sharp knives. Be creative. Cook to music" ~ maxelson
     
Forum Regular
Join Date: Feb 2004
Location: Manhattan
Status: Offline
Reply With Quote
Mar 14, 2004, 11:36 AM
 
Grazie.
I saw a woman with a sweatshirt that said "Guess", so I said, "Implants?"
     
Mac Elite
Join Date: Oct 2000
Status: Offline
Reply With Quote
Mar 14, 2004, 03:57 PM
 
Originally posted by philzilla:
which iTunes are you looking at? it can't be the same one i'm looking at:



(and that blue is #ECF3FE, by the way)
Well darn... my iTunes is broken so I was hoping it followed some sort of consistency between Safari's bookmark list and iChat's buddy list... In that case I guess it really doesn't matter. Although I could argue that iTunes having blue as the first color is because it's an older Carbon application and doesn't follow the new standard "alternating color" Cocoa class
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 09:01 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2