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.

Link to comment
Share on other sites

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";

Link to comment
Share on other sites

.

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.