javascript td bgcolor change

andehlu

this modern love
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?
 
Code:
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,
Code:
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);
}
 
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.
 
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 :p

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.
 
Back
Top