2

Assume I have a page like this

<html>    
    <body>
        <table id="t1">
            <tr><th>Head1</th><th>Head2</th></tr>
            <tr><td>1</td><td>2</td></tr>
            <tr><td>3</td><td>4</td></tr>
      </table>
   </body>
</html>

My CSS

table th { background:color1;} 
table td { background:color2;}

How do I change the background color for all rows except header row with a javascript statement. Is looping through each row and column the only method?

I can get document.getElementById('t1').rows[1] and rows[2] and change background. But is there another efficient way?

2
  • 2
    th and td already have different colors color1 and color2 so aren't they already different? Commented Aug 9, 2012 at 18:06
  • @Annjawn They are! I want to hightlight just the tds with a different color(different than color2) on a particular client click. Commented Aug 9, 2012 at 18:13

4 Answers 4

8

You can loop over the elements and change the background

var els = document.getElementById("t1").getElementsByTagName("td");

for(var i=0;i<els.length;i++){
  els[i].style.background = "green"   
}
Sign up to request clarification or add additional context in comments.

2 Comments

This is what I was planning to do. Was checking if there was some other efficient method.
Looks like this is the only way what you want to do.
3

put a class on the rows and put backgrounds in css like you do with the tables

1 Comment

The number of rows are not known in advance. I would guess it is better to loop through each row than give an id for each row. My question is if I can do this without looping!
3

You can try this:

table th {background: color1;}
table tbody {background: color2;}

<table id="t1">
    <tr><th>Head1</th><th>Head2</th></tr>
    <tbody id="t2">
        <tr><td>1</td><td>2</td></tr>
        <tr><td>3</td><td>4</td></tr>
    </tbody>
</table>

And in the script:

document.getElementById('t2').style.backgroundColor=color3;

EDIT

It's also possible to add a rule to the particular styleSheet.

CSS:

table th {background: color1;}
table td {background: color2;}

Script:

var sSheet = document.styleSheets[0];
if (sSheet.insertRule) {
    sSheet.insertRule('td {background-color: color3}', sSheet.cssRules.length);
} else { // For IE < 9
    sSheet.addRule('td', 'background-color: color3', -1);
}

You can also modify an existing rule itself:

var tdRule,
    sSheet = document.styleSheets[0];       
if (sSheet.cssRules) {
    tdRule = sSheet.cssRules[1];
} else { // For IE < 9
    tdRule = sSheet.rules[1];
}
tdRule.style.backgroundColor = color3;

3 Comments

tbody style would be overidden by td style
@Jeff That's why there is not td style defined in my code :). Ofcourse you can style tds, just not set background for those.
@Jeff I've edited my answer. There are now two more methods available to change background-color of the tds, without any kind of looping.
-1

I use JQuery.

$('td').css('background','#3232');

1 Comment

Even for jquery, the code you've written wont work. Its $(td).css('background','#3232');

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.