onmouseover change css class

Discussion in 'JavaScript' started by adamjblakey, Aug 26, 2008.

  1. #1
    Hi,

    What i want to do is when i mouseover an image it changes the class of a div that is somewhere on the page.

    I thought i could do this by doing:

    
    <div class="header_image2" name="remote" id="remote">
        <div id="header_image2_inner">
    		<img src="images/composite-decking-sm.JPG" height="75" hspace="2" border="0" onmouseover="remote.className = 'composite1';" onmouseout="remote.className = 'header_image2';"/>	
        </div>
    </div>
    
    HTML:
    But this does not work, any ideas?

    Cheers,
    Adam
     
    adamjblakey, Aug 26, 2008 IP
  2. xlcho

    xlcho Guest

    Messages:
    532
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #2
    This will work only if you have a global variable that points to the 'remote' object. Something like this:
    var remote = document.getElementById('remote');
    Code (markup):
    This is the first way to do it. The second one is to change the code you've written to:
    ...
    <img src="images/composite-decking-sm.JPG" height="75" hspace="2" border="0" onmouseover="document.getElementById('remote').className = 'composite1';" onmouseout="document.getElementById('remote').className = 'header_image2';"/>  
    ...
    Code (markup):
    Your code doesn't work, cause when you write just 'remote' the script searches for a variable with that name..
     
    xlcho, Aug 26, 2008 IP
  3. joebert

    joebert Well-Known Member

    Messages:
    2,150
    Likes Received:
    88
    Best Answers:
    0
    Trophy Points:
    145
    #3
    If you plan on using that exact HTML, and don't plan on changing anything about that section any time soon, you could also use "this.parentNode.parentNode" instead of "remote".
     
    joebert, Aug 29, 2008 IP