Is there anything wrong with this SHORT code?

Discussion in 'Javascript' started by Alex, Feb 27, 2006.

  1. Alex

    Alex Guest

    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' );
    }
     
    Alex, Feb 27, 2006
    #1
    1. Advertising

  2. Alex

    Randy Webb Guest

    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.

    --
    Randy
    comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
    Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
     
    Randy Webb, Feb 27, 2006
    #2
    1. Advertising

  3. Alex

    RobG Guest

    Randy Webb wrote:
    > 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>

    --
    Rob
     
    RobG, Feb 27, 2006
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mano kumar
    Replies:
    2
    Views:
    374
    Kevin Spencer
    Oct 17, 2003
  2. Replies:
    5
    Views:
    523
  3. rf
    Replies:
    1
    Views:
    377
    Leif K-Brooks
    Jul 6, 2003
  4. Roubles
    Replies:
    3
    Views:
    356
    Malcolm
    May 14, 2004
  5. Peng Yu
    Replies:
    3
    Views:
    296
    Peng Yu
    Sep 10, 2008
Loading...

Share This Page