troubleshooting Question

jQuery vertical accordion menu problem

Avatar of b24
b24 asked on
JavaScriptJScript
4 Comments1 Solution701 ViewsLast Modified:
Hi Experts,

Apologies for this duplicate entry. I closed a previous question before I realised there was still an issue with it.

I got much appreciated help from expert Zyloch to get to this point.

I have a html list menu (attached). I cannot alter the html in any way. I would like to have the menus expand and collapse on hover.

With the code attached, the menus expand and collapse when you hover on the parent link '#nav li a' but when you hover on the sublinks in that menu, it does not remain expanded.

The menu should remain expanded until you hover over another parent link or remove the mouse from the menu altogether.

Does any one have any ideas how to achieve this?

Many Thanks.
/* html */

<div id="main_menu">
          <ul id="nav">
            <!-- Start of CMS Top Menu Object -->
            <li class="link_selected"><a href="" >H</a></li>
            <li class="link"><a href="" >M</a>
              <ul>
                <li class="link"><a href="" class="enclose">C</a></li>
                <li class="link"><a href="" >L</a></li>
                <li class="link"><a href="" >V1</a></li>
                <li class="link"><a href="" >V2</a></li>
                <li class="link"><a href="" >V3</a></li>
                <li class="link"><a href="" >V4</a></li>
              </ul>
            </li>
            <li class="link"><a href="" >S</a>
              <ul>
                <li class="link"><a href="" class="enclose">C</a></li>
                <li class="link"><a href="" >T</a></li>
                <li class="link"><a href="" >G</a></li>
                <li class="link"><a href="" >I</a></li>
                <li class="link"><a href="" >P</a></li>
                <li class="link"><a href="" >C</a></li>
                <li class="link"><a href="" >M</a></li>
              </ul>
            </li>
            <li class="link"><a href="" >Q</a></li>
            <li class="link"><a href="" class="enclose">C</a></li>
            
            <!-- End of CMS Top Menu Object -->
          </ul>
        </div>


/* CSS */


ul#nav, ul#nav ul 
{
  list-style-type:none;
  padding: 0;
  width: 228px; 
  margin:1px 0px 1px 1px;
}

ul#nav a 
{
  display: block;
  text-decoration: none;	
}

ul#nav li {
  margin-top: 1px;
}

ul#nav li a 
{
	width:197px;
	font-weight: bold;
	color: white;
	background: #005883 url(link_image.jpg) no-repeat 8px 9px; 
	margin-bottom: 1px; 
	padding: 7px 0 0px 31px;
	min-height:24px;
}

ul#nav li a:hover 
{

}

ul#nav li ul li a 
{
  color: #818085;
  background-color: #bcdeef; 
  padding-left: 30px;
  font-weight:normal;
  background-image:none;
  font-size:.9em;
}

ul#nav li ul li a:hover 
{
	text-decoration:underline;
}

.code 
{ 
	border: 1px solid #ccc; 
	list-style-type: decimal-leading-zero; 
	padding: 5px; 
	margin: 0; 
}

.code code 
{ 
	display: block; 
	padding: 3px; 
	margin-bottom: 0; 
}

.code li 
{ 
	background: #ddd; 
	border: 1px solid #ccc; 
	margin: 0 0 2px 2.2em; 
}

.indent1 
{ 
	padding-left: 1em; 
}

.indent2 
{ 
	padding-left: 2em; 
}



/*jQuery*/


function initMenu() {
  $('#nav ul').hide();
  $('#nav ul:first').hide();
$('#nav li a').hover(
    function() {
      var checkElement = $(this).next();
      if((checkElement.is('ul')) && (checkElement.is(':visible'))) {
        return false;
        }
      if((checkElement.is('ul')) && (!checkElement.is(':visible'))) {
        $('#nav ul:visible').slideUp('normal');
        checkElement.slideDown('normal');
        return false;
      }
    },
    function() {
      var checkElement = $(this).next();
      if((checkElement.is('ul')) && (checkElement.is(':visible'))) {
        checkElement.slideDown('normal');
        $('#nav ul:visible').slideUp('normal');
        return false;
      }
    }
  );
  }
$(document).ready(function() {initMenu();});

Open in new window

ASKER CERTIFIED SOLUTION
Vipul Patel
.NET Expert

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 4 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 4 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros