Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 302
  • Last Modified:

How to change the value on attribute in html using javascript

I got the following line of code in html which create a table:

                  <td id="Q6Cell0ID" class="matrixCellBorder" bgcolor="#FFFFFF">&#160;</td>

How using a click of a button to execute a javascript function, I want to change the value of bgcolor from "#FFFFFF" to "#FF0000". To do this I am trying to use the following line in javascript

          document.getElementById('Q6Cell0ID').setAttribute("bgcolor","#FF0000");

I get no javascript errors, but the back ground colour doesnt change. Any suggestions???
0
dipster307
Asked:
dipster307
1 Solution
 
Gurvinder Pal SinghCommented:
try this
document.getElementById('Q6Cell0ID').style.backgroundColor="#FFCC80";

http://www.w3schools.com/jsref/prop_style_backgroundcolor.asp
0
 
kadabaCommented:
Hello

The bgcolor is deprecated.
http://www.w3schools.com/tags/att_td_bgcolor.asp
so avoid using that.
You can use as shown in the code attached.

<html>
<head>
<script type="text/javascript">
function changeTDColor(tdId,tdColor){
	document.getElementById(tdId).style.backgroundColor = tdColor;	
}

</script>
</head>
<body>
<table width="100%">
	<tr>
		 <td id="Q6Cell0ID" class="matrixCellBorder" bgcolor="#FFFFFF">&#160;</td>
		 <td><input type="button" value="change color of adjacent TD" onclick="changeTDColor('Q6Cell0ID','#FF0000');"></td>
	</tr>
</table>
</body>
</html>

Open in new window

0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now