I currently have an alternative table row color with the following javascript code:
function alternate(id){
if(document.getElementsByTagName){
var table = document.getElementById(id);
var rows = table.getElementsByTagName("tr");
for(i = 0; i < rows.length; i++){
if(i % 2 ==0 ){
rows[i].className = "odd0";
}else {
rows[i].className = "even2";
}
}
}
} // end function
What I would like to add in here, is to make the condition of either of the following:
-
If Div id “arrow” exists in the table row, then make the classname “odd0”
-
If Div id “arrow” exists in the table row, then make the classname the same color as the row above it.
My first attempt was to try to make the classname “odd0” if the div id “arrow” exists; however, it just breaks my code or doesn’t read it.
I attempted the following, by either making it into its own function or inserting it within the for loop…
if (document.getElementById("arrow")) {
rows[i].className = "odd0";
}
Any help is much appreciated!
Your question is a bit vague and your requirements seem contradictory. But, I think what you want is for your rows to alternate between
odd0andeven2, unless you have anarrowin your row. Then you want the current row to have the same class as the row above it.Correct me if I am wrong.
Because you put jQuery as a tag, I will give you a jQuery answer. If you don’t know jQuery, I would recommend learning it. It makes this kind of class/css manipulation a lot easier than straight JavaScript, and takes care of all of the cross-browser issues for you.
Demo: http://jsfiddle.net/jtbowden/uBgYH/1/