Change/Add CSS class to parent element on mouseover?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • draney
    New Member
    • Aug 2008
    • 8

    #1

    Change/Add CSS class to parent element on mouseover?

    I have a <div id="nav-left"> and I want a mouseover command that would add a class to that div, like this <div id="nav-left" class="magic">. It would work similar to this example:

    Code:
    <p onmouseover="this.style.color='red'" onmouseout="this.style.color='black'">Move the mouse pointer over this text to change its color. Move the pointer off the text to change the color back.</p>
    Where that code refers to "this" I need to refer to the div element (see my actual HTML below). Also, that code adds an inline style and I would rather add a class, but I could live with adding an inline style for the background image position. I am trying to make the background image sprite for the div reposition when mousing over the links. Of course I want it to go back to normal on mouseout.

    Code:
    <div id="nav-left">
        <ul>
            <li><a id="btn-elves" href="#" title="Magic">Magic Elves</a></li>
            <li><a id="btn-new" href="#" title="New">New</a></li>
            <li><a id="btn-accessories" href="#" title="Accessories">Accessories</a></li>
        </ul>
    </div>
    Last edited by Dormilich; Jul 28 '10, 05:42 PM. Reason: Please use [code] tags when posting code
  • JKing
    Recognized Expert Top Contributor
    • Jun 2007
    • 1206

    #2
    Code:
    	<div id="nav-left">
    		<ul>
    			<li><a id="btn-elves" href="#" title="Magic" onmouseover="getElementById('nav-left').className='magic'" onmouseout="getElementById('nav-left').className='notmagic'">Magic Elves</a></li>
    			<li><a id="btn-new" href="#" title="New">New</a></li>
    			<li><a id="btn-accessories" href="#" title="Accessories">Accessories</a></li>
    		</ul>
    	</div>

    Comment

    • draney
      New Member
      • Aug 2008
      • 8

      #3
      Originally posted by JKing
      Code:
      	<div id="nav-left">
      		<ul>
      			<li><a id="btn-elves" href="#" title="Magic" onmouseover="getElementById('nav-left').className='magic'" onmouseout="getElementById('nav-left').className='notmagic'">Magic Elves</a></li>
      			<li><a id="btn-new" href="#" title="New">New</a></li>
      			<li><a id="btn-accessories" href="#" title="Accessories">Accessories</a></li>
      		</ul>
      	</div>
      Thanks!! It is perfect.

      Comment

      Working...