views:

64

answers:

1

This is just a really novice question on jQuery but I'm just using it to create a simple accordion menu. The HTML ouput is like this:

<ul id="nav-sub">
<li class="sub-level-0"><a href="#">Menu Item One</a></li>
<li class="parent-here last sub-level-0"><a href="#">Menu Item Two</a>   
    <ul>
        <li class="here sub-level-1"><a href="#">Sub Menu Item One</a></li>
        <li class="last sub-level-1"><a href="#">Sub Menu Item Two</a></li>
    </ul>  
</li>                                                                                        

And the jQuery I currently have is:

$(document).ready(function() {

// Show the children of the first product on page load but leave the others hidden
$("ul#nav-sub li.parent-here ul").show();

// Then attach a visibility toggle to each of the parents 
if ( $("ul#nav-sub li.sub-level-0 ul").size > 0 ) {
    $("ul#nav-sub li.sub-level-0 > a").click(function(){
        $(this).next().slideToggle("slow");
        return false; //Prevent the browser jump to the link anchor
    });
}

});

This is the closest I can get it to fully working but the only thing that doesn't work is the toggle animation. Instead of a slow transition it simply jumps right open.

What I basically want is to get the slow transition effect but ALSO to only return false (prevent the default browser action of following the link anchor) if the menu item has any children (a sub list - as shown above on Menu Item Two). I need Menu Item One to return true and go straight to that page.

Thanks

A: 

YAY!! I got it!!! haha ... Yea Im like you and like to figure things out to learn...I thought it was going to be easy but jeez I spent 30 mins debugging the damn thing.. I was about to post another question about my answer to you question on SO... then i found the ^&*^#$, it was a <ul/>I typed in! =(

Im so happy and powerful now... happiness and power to everybody =)

See http://jsfiddle.net/QcBNK/92/

Html:

<ul id="nav-sub">
    <li class="sub-level-0"><a href="#">Menu Item One</a>
    <ul>
        <li class="sub-level-1"><a href="#">Sub Menu Item One</a></li>
        </ul>
      </li>
     <li class="sub-level-0"><a href="#">Menu Item Two</a>   
      <ul>
                <li class="sub-level-1"><a href="#">Sub Menu Item One</a></li>
                <li class="sub-level-1"><a href="#">Sub Menu Item Two</a></li>
      </ul>  
      </li>    
</ul>

JQuery :

$(document).ready(function() {
    //hide all SUB elements
    $(".sub-level-1").hide(); 
    //attach click to top elements only
    $("li.sub-level-0").mousedown(function(evt) {
        //find the sub element and toggle it..
       if(($(evt.target).text().indexOf("Sub"))!=0) {//Bad hack! sry =(
        $(this).find("ul .sub-level-1").slideToggle();
      }
    });

});

Notes: Im no expert.. I noticed later that the sub items elements throw the event, and oddly enough find() returns a child <ul> even if the sub element threw the event!? So I could not use evt.target==this to test for a bubbled event. The sub elements event returns the entire parent li! =S So I threw in a haack! Maybe there is a better method.

giddy
Yeah I know, it's something that seems pretty easy on the surface but fine-tuning it is a bit harder to get right. I've given your solution a try though but I don't always get the animation effects on all menus and when I click on a sub menu item the list slides back up again. By the way, why did you use .mousedown instead of .click?
Ian
oh the code on that link is not updated to the one here in this answer. See http://jsfiddle.net/QcBNK/92/ I changed to mousedown while I was debugging, I forgot to change it back when it all works. Makes no diff though, although I can't say from a perf. point of view.
giddy
I've tried the code from the link you sent but unfortunately it's still not there. It isn't preventing the click through on the parent items and when clicking on a child link it slides back up when it should stay open.
Ian
Are you sure? I checked the link in my previous comment and the answer (Same thing) in IE8, FF3 and Chrome!? The Menu slides out the sub menus, and the sub menus don't cause the menu to collapse. It does take a few seconds till everything loads up. See [screenie] (http://www.mediafire.com/i/?tw6vw96j5pfiic6)
giddy
Hmm was wondering whether there a difference in real html files and jsfiddle.. but NOPE.. tried it on my PC on IE8, Chrome7 and FF3 works perfectly fine!? See http://pastebin.com/ReiYR09R
giddy