Jump to content

[SOLVED] Alternate row color with DOM/JavaScript - Shouldn't this work?


jb60606

Recommended Posts

This little snippet of code is from a AJAX/PHP/JavaScript widget that retrieves stock quotes and fills in a table with the data.

 

From the JavaScript file:

        for (var i = 0; i < oResult.stocks.length; i++) {
            var altcolor = i % 2 == 0 ? 'light' : 'dark';
            var oStock = oResult.stocks[i];

            var oRow = this.table.insertRow(i);
            oRow.className = "altcolor";

From the CSS file:

.light {
background:#000000;
}
.dark {
background-color:#FFFFFF;
}

 

I literally just started learning DOM and JavaScript about 4 hrs ago, but shouldn't the above snippet work? It works in another plain JavaScript file that I have. I've also tried a few other similar methods I found on the web, though, they didn't see to work either.

 

Thanks in advance.

I think I should mention that when I similarily add a class name to a table cell, like below, it works fine. It seems as though it doesn't recognize what I want to do with "oRow.className =", but understands tdSymbol.className = "ajaxStockWatchlist-stockSymbol"; just fine.

 

            var oRow = this.table.insertRow(i);
            oRow.className = "altcolor";
            
            //Add a cell for the stock's symbol
            var tdSymbol = oRow.insertCell(0);
            tdSymbol.className = "ajaxWatchlist-stockSymbol";

.

light {
background:#000000;
}
.dark {
background-color:#FFFFFF;
}

use 'background-color' in the first one

 

if that is not it, then do you have a class attached to the td's in the inserted row, that would overrule any class attached to the tr's

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.