Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Jul 2006
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Problem with <TR> Tag

    Hello Forum:

    I have a question regarding the following code snippet:

    Code:
      var rows=tableobj.getElementsByTagName('tr');
      for(i=0;i<rows.length;i++){
        rows[i].onmouseover = function(){this.style.backgroundColor="gold";}
        rows[i].onmouseout = function(){this.style.backgroundColor="#FFFFFF";}
      }
    This peice of code will not work, no matter what I try. I have verified that there are 20 <tr> tags in tableobj. If I change the code to
    Code:
    var rows=tableobj.getElementsByTagName('td');
    it works properly (by highligting the individual cells.) What am I doing wrong? Thanks.

    Shawn

  • #2
    Senior Coder
    Join Date
    Mar 2005
    Location
    Portsmouth UK
    Posts
    4,503
    Thanks
    3
    Thanked 500 Times in 487 Posts
    as expected this works for me
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    
    <head>
      <title></title>
    <script language="JavaScript" type="text/javascript">
    /*<![CDATA[*/
    function Add(){
      var tableobj=document.getElementById('fred');
      var rows=tableobj.getElementsByTagName('TR');
      for(i=0;i<rows.length;i++){
        rows[i].onmouseover = function(){this.style.backgroundColor="gold";}
        rows[i].onmouseout = function(){this.style.backgroundColor="#FFFFFF";}
      }}
    /*]]>*/
    </script></head>
    
    <body onload="Add();">
    <table id="fred" cellpadding="0" cellspacing="0" border="1">
      <tr>
        <td width="100" >tttttttt</td>
      </tr>
    </table>
    </body>
    
    </html>
    Vic

    God Loves You and will never love you less.

    http://www.vicsjavascripts.org/Home.htm

    If my post has been useful please donate to http://www.operationsmile.org.uk/


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •