Jeff Jeff - 1 year ago 66
Javascript Question

Setting the style property for table rows using javascript

Assume I have a page like this

<table id="t1">


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
and change background. But is there another efficient way?

Answer Source

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"   
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download