1

I have a table like this:

info1    yes    no    yes    yes
info2    no     yes   no     yes
info3    no     yes   no     no
                ...

I could write some code (see below) that is working fine to set a green/red background if I see yes/no. But now I would like to have a different green/red for the even/odd rows.

Any idea how I can change the code below so I have different green/red for odd/even rows?

var x = document.getElementsByTagName("TD")
var i=0; 
for (i=0;i<x.length;i++) 
{ 
  if (x[i].className=="tdStyle")
  { 
    if (x[i].innerHTML=="Yes") 
    { 
      x[i].style.backgroundColor='LightCoral'; 
      x[i].style.Color='LightCoral'; 
      x[i].style.border='1px solid grey';
      $(x[i]).html('&nbsp;'); 
    } 
    if (x[i].innerHTML=="No") 
    { 
      x[i].style.backgroundColor='lightgreen'; 
      x[i].style.Color='lightgreen';
      x[i].style.border='1px solid grey';
      $(x[i]).html('&nbsp;');
    } 
  } 
}

As Dhaval Marthak asked, I post js fiddle: http://jsfiddle.net/T8Xe9/

endeka
  • 131
  • 1
  • 15

2 Answers2

2

As there are some other answers with jQuery and CSS, I'll contribute a fully javascript solution. As mentioned by @DhavalMarthak you can use the modulus operator % to know wether a number is odd or even (by dividing by 2, and see if there's a remainder).

So to add to your code it'll look something like:

+ function () {
var tables = document.getElementsByTagName('table');
for (var k = 0; k < tables.length; k++) {
    var rows = tables[k].getElementsByTagName('tr');
    for (var j = 0; j < rows.length; j++) {
        var x = rows[j].getElementsByTagName("TD")
        for (var i = 0; i < x.length; i++) {
            if (x[i].className == "tdStyle") {
                if(j % 2 == 0 ){  //even row
                    if (x[i].innerHTML == "Yes") {
                        //td style for even yes
                    }
                    if (x[i].innerHTML == "No") { 
                        //td style for even no
                    }
                } else { //odd row
                    if (x[i].innerHTML == "Yes") {
                        //td style for odd yes
                    }
                    if (x[i].innerHTML == "No") { 
                        //td style for odd no
                    }
                }
            }
        }
    }
}}()

Demo


That said, I would go with styling with classes and CSS rather than directly in code. An advantage to this is that you can reuse your code and theme/style it for different pages by simply switching css files. So if we use the :nth-child selector which seems to be relatively compatible, we have, for javascript:

    var x = document.getElementsByTagName("TD")
    for (var i = 0; i < x.length; i++) {
        if (x[i].className == "tdStyle") {
            x[i].setAttribute("data",x[i].innerHTML)
            x[i].innerHTML="&nbsp";
        }
    }

and for CSS:

.tdStyle[data=Yes]{
    background:lightcoral;
}

.tdStyle[data=No]{
    background:lightgreen;
}

tr:nth-child(2n) .tdStyle[data=Yes]{
    background:red;
}

tr:nth-child(2n) .tdStyle[data=No]{
    background:green;
}

Demo

mfirdaus
  • 4,574
  • 1
  • 25
  • 26
  • I can't add CSS. It's an existing table in a program and I can only change the layout by code like above. At the moment your code is not working. I don't know why... – endeka May 27 '14 at 11:41
  • @endeka Hm.. would it be possible to post the html of the tables? And are there more than one tables per page? – mfirdaus May 27 '14 at 11:46
  • The code is "working" now. The problem is that he see every row as odd... It's a SharePoint table. Where can I post the html? On jsfiddle? But maybe that SharePoint read the code different... – endeka May 27 '14 at 11:52
  • I guess if you have snippet you can add it to your question. I would probably try and select each table, then `table.getElementsByTagName("tr")` from there. Maybe there is a class attached to the table? Firefox has a "view selected source" if you select the table which should see the dynamic table. Similarly you can select an element in web inspector and copy that in chrome. – mfirdaus May 27 '14 at 12:02
  • the code of the table is very long, so I don't think it's a good idea to post it here. At the moment de browser doesn't see a difference between odd and even rows. it always takes the last 'x[i].innerHTML == "Yes"' or 'x[i].innerHTML == "No"' – endeka May 27 '14 at 12:32
  • @endeka not sure. Try my edit, where I go table by table. I enclosed everything in an anonymous function just incase there's any clash too. but honestly I'm not sure what could be wrong at this stage without more information. – mfirdaus May 27 '14 at 13:13
  • I added the class name of my table and now it works! Tnx for the help! So I don't use your "+function ()..." – endeka May 27 '14 at 14:06
0

Here is a Demo Fiddle

If you are using jQuery, use this:

$('table td.tdStyle:contains("yes")').css({
    "background-color":"lightgreen",
    "border":"1px solid grey",
    "color":"lightgreen"
});
$('table td.tdStyle:contains("yes")').html('&nbsp;');

$('table td.tdStyle:contains("no")').css({
    "background-color":"lightcoral",
    "border":"1px solid grey",
    "color":"lightcoral"
});
$('table td.tdStyle:contains("no")').html('&nbsp;');

The selector is based on td which contain yes or no


For odd/even,

$('table td.tdStyle:odd').css({
    "background-color":"lightgreen",
    "border":"1px solid grey",
    "color":"lightgreen"
});
$('table td.tdStyle.tdStyle:odd').html('&nbsp;');

$('table td.tdStyle:even').css({
    "background-color":"lightcoral",
    "border":"1px solid grey",
    "color":"lightcoral"
});
$('table td.tdStyle:even').html('&nbsp;');

The selector is based on alternate odd and even td.

Shaunak D
  • 20,588
  • 10
  • 46
  • 79
  • This does not actually answer the question as it does not use OP's code and does not provide 4 different colors. – Artjom B. May 27 '14 at 10:23
  • @shaunakde. This is not working. I need to slect on yes/no AND even/odd. All the yes/odd, yes/even, no/odd, no/even need to have a color – endeka May 27 '14 at 11:03
  • Can you elaborate the color combinations/events on yes/odd, yes/even, no/odd and no/even each? – Shaunak D May 27 '14 at 11:05