2

I have a table that has dynamic columns added to it by Javascript. I am looking for a way to alternate the background color of every two columns, such as:

<table>
  <tr>
    <td>FIRST BGCOLOR</td>
    <td>FIRST BGCOLOR</td>
    <td>SECOND BGCOLOR</td>
    <td>SECOND BGCOLOR</td>
    <td>FIRST BGCOLOR</td>
    <td>FIRST BGCOLOR</td>
    <td>SECOND BGCOLOR</td>
    <td>SECOND BGCOLOR</td>
  </tr>
</table>

So I know how to do alternate coloring with CSS (:nth-child(odd) etc..), but not how to have TWO columns the same color, then different color for the next two, and so on...

How can this be acheived? Javascript or jQuery solutions are also welcomed as I am using those already in the project, but CSS would be preferrable of course.

Thank you all!

1
  • Normally when someone wants alternating styles, you'd use some nth-child magic, but in this case I think JavaScript might be necessary. Commented Nov 5, 2014 at 15:24

4 Answers 4

6
td
{
    background: #0f0;
}
td:nth-child(4n+3),
td:nth-child(4n+4)
{
    background: #f00;
}
Sign up to request clarification or add additional context in comments.

Comments

6

td{
  background: grey;
  }

td:nth-of-type(4n), td:nth-of-type(4n - 1){
  background: lime;
  }
<table>
  <tr>
    <td>FIRST BGCOLOR</td>
    <td>FIRST BGCOLOR</td>
    <td>SECOND BGCOLOR</td>
    <td>SECOND BGCOLOR</td>
    <td>FIRST BGCOLOR</td>
    <td>FIRST BGCOLOR</td>
    <td>SECOND BGCOLOR</td>
    <td>SECOND BGCOLOR</td>
  </tr>
</table>

Comments

0

I believe that you will be generating columns using an JQuery event.

So what i would do is to run the function below at the end of your event:

function changeTDColor()
{
    var colorIntivalCount = 2; //how many times of repeat for each color
    var tdCount = 0;
    var currentColor = "#FFFFFF";  //starting from white for example
    var nextColor= "#000000";
    var tempColor= null; //this one is for temporary storing


    $('td').each(function(){
        $(this).css('background-color', currentColor)
        tdCount += 1;
        if(tdCount >= colorIntivalCount) //every time it reach intival, exchange
        {
           tempColor = currentColor;
           currentColor = nextColor;
           nextColor = tempColor; //exchange current color and next color
           tdCount = 0;           //reset the count
        }
     });
}

So in this way, every time you generate a new set of columns, the color will be reset.

Hope this helps.

Comments

0

Try

var colors = ["lightblue", "lightgreen"]
, elem = $("table tr td");
elem.map(function(i, el) {
    $(el).is(":even") 
    ? elem.slice(i, i + 2)
      .css("backgroundColor", colors[0]) 
      && colors.reverse() 
    : null
})

var colors = ["lightblue", "lightgreen"]
, elem = $("table tr td");
elem.map(function(i, el) {
    $(el).is(":even") 
    ? elem.slice(i, i + 2)
      .css("backgroundColor", colors[0]) 
      && colors.reverse() 
    : null
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<table>
  <tr>
    <td>FIRST BGCOLOR</td>
    <td>FIRST BGCOLOR</td>
    <td>SECOND BGCOLOR</td>
    <td>SECOND BGCOLOR</td>
    <td>FIRST BGCOLOR</td>
    <td>FIRST BGCOLOR</td>
    <td>SECOND BGCOLOR</td>
    <td>SECOND BGCOLOR</td>
  </tr>
</table>

3 Comments

THanks for this, though I found it easier using CSS in this case.
What if I want to have a column a red, two columns blue, a column red, two colums blue, and so on? Can you help me with this, please?
@Anonymous Have you posted a Question including html, css, describing expected result?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.