views:

312

answers:

5

I'm making a vertical navigation menu using css and jquery to make the submenus hidden by default but clicking on a menu item expands it to show the submenus.

<div id="navmenu">


<ul id="menu">
  <li><a href="welcome.html" target="content">Welcome</a>
    <ul class="hide">
     <li><a href="other.php" target="content">blank</a> </li>    
    </ul>  
  </li>

  <li><a href="view_form.php" target="content">Student Nurse</a></li>
  <li><a href="http://www.google.com" target="content">Internet</a></a></li>
  <li><a href="http://support.site" target="content">Support</a></li>
  <li><a href="">Policies</a>
   <ul class="hide">
    <li><a href="shared/Policies/ContactList.txt" target="content">Policy 1</a></li>   
   </ul>  
  </li>  
 </ul>
</div>

the jquery to make the submenus hide and show:

$('#menu li').css("margin-left","20px");

$('#menu li').toggle(
 function() {
            $(this).find('ul').show();
     },
     function() {
           $(this).find('ul').hide();
 });

This code works perfectly for expanding and collapsing the submenus, however, none of the links work now? I don't understand what I'm missing?

edit: firebug output:

<div id="navmenu">
   <ul id="menu">
    <li style="margin-left: 20px;">
       <a target="content" href="welcome.html">Welcome</a>
       <ul class="hide">
         <li style="margin-left: 20px;">
            <a target="content" href="view_form.php">a blank one here</a>
         </li>
       </ul>
     </li>
     <li style="margin-left: 20px;">
         <a target="content" href="view_form.php">Student Nurse</a>
     </li>
     <li style="margin-left: 20px;">
        <a target="content" href="http://www.google.com"&gt;Internet&lt;/a&gt;
     </li>
     <li style="margin-left: 20px;">
       <a target="content" href="http://support.site"&gt;Support&lt;/a&gt;
     </li>
     <li style="margin-left: 20px;">
        <a href="">Policies</a>
        <ul class="hide">
          <li style="margin-left: 20px;">
            <a target="content" href="shared/Policies/ContactList.txt">Policy 1</a>
          </li>
        </ul>
      </li>
    </ul>
  </div>

so the links are correct even when the submenu's are expanded but they are still broken.

A: 

The toggle seems to override the link, you might need find a more specific filter to only toggle those with submenus.

Steven Pardo
but even the submenu links are broken. Its as if they aren't links at all anymore.
controlfreak123
Use firebug to find out for sure. Or post a live link for us to check.
Pekka
A: 

Could it be that the links all work, but they're refreshing a different, specific browser window (one named "content") somewhere in your task bar?

Pekka
no. THe page has a frame that contains this menu in a frame on the left and then the "content" frame is on the right of the page and it contains whatever the links point to. As I said in my comment above I know the links work when i take away the jquery but that defeats my purpose because then I don't have expanding menus
controlfreak123
All right. Can you use firebug to see what happpens to the `a` elements? Like, whether they lose their `href`, or something else moves on top of them that makes them unclickable?
Pekka
A: 

Try it using

$('#menu li').toggleClass('hidden', 0);

OFC you must have a css class called hidden that contains display:none;

or something along those lines, depending on what you want to hide

Mathias Nielsen
this gives me the same result as what I had before. The submenus expand and contract when you click on a menu item but clicking a submenu item does not take you to the link
controlfreak123
+2  A: 

You need to enable the default behavior of the 'a' with return true;. I added some classes to your HTML, and changed up the javascript a little.

EDIT: Deleted solution, because other problems cropped up.

I did find that if you add a click event handler to 'a', it will load the page. The event handler doesn't need to do anything at all.

patrick dw
+2  A: 

try the following:

change the menu to:

<div id="navmenu">
 <ul id="menu">
  <li><div><a href="welcome.html" target="content">Welcome</a></div>
    <ul class="hide">
     <li><a href="other.php" target="content">blank</a> </li>    
    </ul>  
  </li>

  <li><a href="view_form.php" target="content">Student Nurse</a></li>
  <li><a href="http://www.google.com" target="content">Internet</a></a></li>
  <li><a href="http://support.site" target="content">Support</a></li>
  <li><div><a href="">Policies</a></div>
   <ul class="hide">
    <li><a href="shared/Policies/ContactList.txt" target="content">Policy 1</a></li>   
   </ul>  
  </li>  
 </ul>
</div>

and then change your javascript to the following:

$('#menu li:has(ul) > div').toggle(
    function() {
        $(this).next().show();
    },
    function() {
        $(this).next().hide();
    }
);
Steven Pardo
this worked spectacularly! I get what you are doing but I'm curious why adding the div tags made the links work again?
controlfreak123
The toggle function works when you click the contents (html) of the tag you are attaching the function to. Therefore you needed to root the link. You can actually wrap all of the links in div tags.
Steven Pardo