Results 1 to 5 of 5

Thread: change background on hover

  1. #1
    Join Date
    Nov 2007
    Posts
    151
    Thanks
    67
    Thanked 0 Times in 0 Posts

    Default change background on hover

    Hi,

    I have a long table and I want to change the tr's background onmouse hover.

    I know how to do it with CSS but it doesn't work well on some browsers, so I'm looking for a JS solution.

    Thanks!

  2. #2
    Join Date
    Dec 2008
    Location
    Portsmouth, UK
    Posts
    1,891
    Thanks
    2
    Thanked 441 Times in 435 Posts

    Default

    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  type="text/javascript">
    /*<![CDATA[*/
    var Lst=false;
    
    function HighLight(c){
     var evt=window.event||arguments.callee.caller.arguments[0];
     if (Lst){
      Lst.style.backgroundColor='transparent';
     }
     var obj=window.event?evt.srcElement:evt.target;
     if(obj.nodeType==3) obj=obj.parentNode; // defeat Safari bug
     while (obj.parentNode&&obj.nodeName.toUpperCase()!='TR'){
      obj=obj.parentNode;
     }
     if (obj.nodeName.toUpperCase()=='TR'){
      Lst=obj;
      Lst.style.backgroundColor=c;
     }
    }
    /*]]>*/
    </script></head>
    
    <body>
    <table width="100" border="1" onmouseover="HighLight('red');">
      <tr>
       <td>&nbsp;</td>
      </tr>
      <tr>
       <td>&nbsp;</td>
      </tr>
      <tr>
       <td>&nbsp;</td>
      </tr>
      <tr>
       <td>&nbsp;</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/

  3. The Following User Says Thank You to vwphillips For This Useful Post:

    d-machine (10-10-2009)

  4. #3
    Join Date
    Nov 2007
    Posts
    151
    Thanks
    67
    Thanked 0 Times in 0 Posts

    Default

    Hi thank you very much!
    Is it possible to make it change the backgorund of all the td's in the row on hover
    instead of changing the tr's background?
    Since in my table my tr's already have bbackground and I want to keep it.
    My table contains 7 cell in each line.
    Last edited by d-machine; 10-10-2009 at 07:54 PM.

  5. #4
    Join Date
    Mar 2006
    Posts
    12
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default

    Quote Originally Posted by d-machine View Post
    Hi thank you very much!
    Is it possible to make it change the backgorund of all the td's in the row on hover
    instead of changing the tr's background?
    Since in my table my tr's already have bbackground and I want to keep it.
    My table contains 7 cell in each line.
    Not sure I understand exactly what you mean, but I presume you want something like this:
    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  type="text/javascript">
    /*<![CDATA[*/
    var Lst=false;
    
    function HighLight(e,c){
     var e=e||event;
     if (Lst){
      Lst.style.backgroundColor='';
     }
     var obj=e.srcElement||e.target
       Lst=obj; 
       if(Lst.tagName.match(/td/i)){
       Lst.style.backgroundColor=c;}
     }
    
    /*]]>*/
    </script>
    </head>
    
    <body>
    <table width="100" border="1" onmouseover="HighLight(event, 'red');">
      <tr>
       <td>&nbsp;</td><td>&nbsp;</td>
      </tr>
      <tr>
       <td>&nbsp;</td><td>&nbsp;</td>
      </tr>
      <tr>
       <td>&nbsp;</td><td>&nbsp;</td>
      </tr>
      <tr>
       <td>&nbsp;</td><td>&nbsp;</td>
      </tr>
    </table>
    </body>
    </html>
    I've just modified vwphillips code a bit...

  6. #5
    Join Date
    Dec 2008
    Location
    Portsmouth, UK
    Posts
    1,891
    Thanks
    2
    Thanked 441 Times in 435 Posts

    Default

    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  type="text/javascript">
    /*<![CDATA[*/
    var Lst=false;
    
    function HighLight(c){
     var evt=window.event||arguments.callee.caller.arguments[0];
     if (Lst){
      for (var z0=0;z0<Lst.length;z0++){
       Lst[z0][0].style.backgroundColor=Lst[z0][1];
      }
     }
     var obj=window.event?evt.srcElement:evt.target;
     if(obj.nodeType==3) obj=obj.parentNode; // defeat Safari bug
     while (obj.parentNode&&obj.nodeName.toUpperCase()!='TR'){
      obj=obj.parentNode;
     }
     if (obj.nodeName.toUpperCase()=='TR'){
      Lst=[];
      var tds=obj.cells;
      for (var z0=0;z0<tds.length;z0++){
       Lst.push([tds[z0],zxcSV(tds[z0],'background-Color')]);
       tds[z0].style.backgroundColor=c;
      }
     }
    }
    
    function zxcSV(obj,par){
     if (obj.currentStyle) return obj.currentStyle[par.replace(/-/g,'')];
     return document.defaultView.getComputedStyle(obj,null).getPropertyValue(par.toLowerCase());
    }
    
    
    /*]]>*/
    </script></head>
    
    <body>
    <table width="100" border="1" onmouseover="HighLight('red');">
      <tr>
       <td>&nbsp;</td>
       <td>&nbsp;</td>
      </tr>
      <tr>
       <td style="background-Color:blue;">&nbsp;</td>
       <td>&nbsp;</td>
      </tr>
      <tr>
       <td>&nbsp;</td>
       <td>&nbsp;</td>
      </tr>
      <tr>
       <td>&nbsp;</td>
       <td style="background-Color:green;">&nbsp;</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/

Bookmarks

Posting Permissions

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