0

I've been battling with this piece of code the last few days and I really don't know what to do with it.

Basically when a user rolls their mouse over one of the LI elements from the main nav menu, a Javascript function runs and depending on which element triggered the function puts links into the UL underneath (id=mainNav_Drop).

What I want to do is set a style of

background-color: #000 

on the LI element from the main nav both when it is hovered over and the relevant content in the sub-menu is present (not ONLY when it's hovered over), and then for this styling to be removed and switched between different LI elements as they are each rolled over respectively.

Please help I'm at a dead loss, here's the code.

<ul>
    <li class="mainNav_Home" onmouseover="mainNav_Hover('Home')"><a href="#">Home</a></li>
    <li class="mainNav_Portfolio" onmouseover="mainNav_Hover('Portfolio')"><a href="#">Portfolio</a></li>
    <li class="mainNav_Tutorials" onmouseover="mainNav_Hover('Tutorials')"><a href="#">Tutorials</a></li>
    <li class="mainNav_Contact" onmouseover="mainNav_Hover('Contact')"><a href="#">Contact</a></li>
    <ul id="mainNav_Drop">
            <li class='mainNav_Drop_Home'><a href='#'></a></li>
    </ul>
</ul>

function mainNav_Hover(cls){
    var id=document.getElementById('mainNav_Drop');
    switch(cls){
        case("Home"):
            id.innerHTML="<li class='mainNav_Drop_Home'><a href='#'>Home</a></li>"
            break;
        case("Portfolio"):
            id.innerHTML="<li class='mainNav_Drop_Portfolio'><a href='#'>Qualifications</a></li><li class='mainNav_Drop_Portfolio'><a href='#'>Services</a></li><li class='mainNav_Drop_Portfolio'><a href='#'>Portfolio</a></li><li class='mainNav_Drop_Portfolio'><a href='#'>Case Studies</a></li>"
            break;
        case("Tutorials"):
            id.innerHTML="<li class='mainNav_Drop_Tutorials'><a href='#'>HTML5</a></li><li class='mainNav_Drop_Tutorials'><a href='#'>CSS3</a></li><li class='mainNav_Drop_Tutorials'><a href='#'>WordPress</a></li><li class='mainNav_Drop_Tutorials'><a href='#'>Design</a></li>";
            break;
        case("Contact"):
            id.innerHTML="<li class='mainNav_Drop_Contact'><a href='#'>Contact</a></li>"
            break;
        default:
            id.innerHTML="<li><a href='#'></a></li>"
        }
}
HoldOffHunger
  • 18,769
  • 10
  • 104
  • 133
aaa
  • 13
  • 1
  • 8
  • To clarify: if the user hovers / moves the mouse over the menu item home you want background of `
  • – surfmuggle Jul 14 '13 at 05:02
  • That looks like a job for [jqueryUI](http://jqueryui.com/themeroller/). – havarc Jul 14 '13 at 08:23
  • May be would be better hide content and with css toggle classes instead of creating new content of `"mainNav_Drop"` over and over... – Givi Jul 14 '13 at 09:00
  • http://jsfiddle.net/GKDev/StG32/ try this... – Givi Jul 14 '13 at 09:26
  • @Givi your jsFiddle doesn't work I appreciate the attempt though. – aaa Jul 15 '13 at 06:00
  • @threeFourOneSixOneThree currently if the user hovers over for example class="mainNav_Home" the background colour is triggered by the CSS :hover selector in my CSS. However, when the user hovers over class="mainNav_Drop" the mainNav_Home (it could also be any other of the 4 main nav links) comes out of its hover state. I want to implement a constant hover state on the main navigation link once it's been hovered and for this hover state to deactivate once one of the other links is hovered, however I can't use onmouseout() because the point is for the dropdown to stay visible - – aaa Jul 15 '13 at 06:02
  • - once it's activated, as well as the hover state, so it wouldn't work! – aaa Jul 15 '13 at 06:07