Sunday, October 28, 2012

Javascript table red if negative

"How do I make a table show red in a cell if negative?"

There are likely a few things to watch out for (a minus in a text name or something like that.)


        //quick cheat red if negative.
        var table = document.getElementById("my_table");
        for (var j=0, row; row=table.rows[j]; j++) {
                for (var i=0, cell; cell= row.cells[i]; i++) {
                        if (cell.innerText.indexOf("-")>-1) {
                                cell.style.color="red";
                        }
                }
        }

Blog Archive