odie’s webmemory

Entries categorized as ‘dom’

ElementNode.setAttribute(name, value)

December 11, 2006 · Leave a Comment

I have to make an html table sortable by any column. I found the following javascript library that does the trick: “sorttable: make all your tables sortable

There is one problem: my table has alternating colors for the rows. After the sort, some adjacent rows have the same color.

Solution: After the sort, set the color to alternating colors.

var sortedRows = document.getElementsByTagName(“tr”);
for (var i=0; i<sortedRows.length; i++) {
    if (i%2) { sortedRows[i].setAttribute(“bgcolor”, “white”); }
    else { sortedRows[i].setAttribute(“bgcolor”, “silver”); }
}

Categories: dom