# javascript td bgcolor change



## andehlu (Mar 14, 2005)

i know, i know, change the div background with CSS. Id like to, but I wish clients would stop using IE....  Does anyone know how to change a TD bgcolor with javascript?


----------



## HateEternal (Mar 15, 2005)

```
var thetd = document.getElementById('td'); //or whatever
td.style.backgroundColor = 'color' //pretty sure this works
or
td.style.setAttribute('background-color', 'color'); // sure this works
```

those wont work in mozilla/safari so make a seperate IE path :-/

for example, this is what I did,

```
function changeColor(item, color)
{
	//I HATE IE!!!!!!!!!!!!
	var name = /Explorer/;
	if(name.test(navigator.appName))
	{
		//first off all you should get a real browser http://www.mozilla.org
		//is a good place to start
		item.style.setAttribute('background', color);
	}
	else
		item.setAttribute('style', 'background: ' + color);
}
```


----------



## andehlu (Mar 15, 2005)

right on...      <td bgcolor="#666666" onMouseOver="this.style.backgroundColor='#cccccc'" onMouseOut="this.style.backgroundColor='#666666'">&nbsp;</td>

seems to work on safari, firefox on mac and IE pc... thanks hateeternal.


----------



## HateEternal (Mar 15, 2005)

andehlu said:
			
		

> right on...      <td bgcolor="#666666" onMouseOver="this.style.backgroundColor='#cccccc'" onMouseOut="this.style.backgroundColor='#666666'">&nbsp;</td>
> 
> seems to work on safari, firefox on mac and IE pc... thanks hateeternal.



thats a lot easier 

I thought style wasn't a valid Object in Mozilla... but I could be wrong. If that is the case I have no idea why I ever did it the way I did.


----------

