Is there anything wrong with this SHORT code?

A

Alex

Trying to change a few tags by swapping color values to my own. It
doesn't give any error, yet it doesn't perform. Is there an error in my
logic somewhere?

var my_i = document.getElementsByTagName( 'td' );
for (i = 0; i < my_i.length; i++)
{
if ((my_i.hasAttribute( 'BGCOLOR' )) && (my_i.getAttributeNode(
'BGCOLOR' ).value == '#F7FFFB' ))
my_i.setAttribute( 'BGCOLOR', '#00FF00' );
}
 
R

Randy Webb

Alex said the following on 2/27/2006 12:34 AM:
Trying to change a few tags by swapping color values to my own. It
doesn't give any error, yet it doesn't perform. Is there an error in my
logic somewhere?

var my_i = document.getElementsByTagName( 'td' );
for (i = 0; i < my_i.length; i++)
{

if (my_i.style.backgroundColor == '#f7fffb')
{
my_i.style.backgroundColor = '#00ff00';
}

You may want to use toLowerCase() on the backgroundColor if you are not
sure of the Case of the color codes.
 
R

RobG

Randy said:
Alex said the following on 2/27/2006 12:34 AM:
Trying to change a few tags by swapping color values to my own. It
doesn't give any error, yet it doesn't perform. Is there an error in my
logic somewhere?

var my_i = document.getElementsByTagName( 'td' );
for (i = 0; i < my_i.length; i++)
{


if (my_i.style.backgroundColor == '#f7fffb')
{
my_i.style.backgroundColor = '#00ff00';
}

You may want to use toLowerCase() on the backgroundColor if you are not
sure of the Case of the color codes.


Probably worth pointing out to the OP that this will read the background
colour of the TD style object, not the colour inherited through a CSS
stylesheet rule or other declaration.

e.g.

<table style="background-color: #f7fffb;" border="1">
<tr>
<td onclick="alert(this.style.backgroundColor);">
click to see background color <b>inherited</b> from table
</td>
<td style="background-color: #f7fffb;"
onclick="alert(this.style.backgroundColor);">
click to see background color set <b>inline</b>
</td>
</tr>
</table>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top