0

I'm trying to get a jquery slide menu working, but I'm out of ideas right now. How can I stop the menu collapsing when clking on a submenu entry?

Here's what I've got so far - js-part, navigation code, and finally the css part that is relevant:

    <script type="text/javascript">
        $(document).ready(function() {
         $('ul .sub').hide();
         $("li:has(.sub)").click(function() {
            $("ul", this).toggle('slow');
        });
    </script>

    <nav id="nav" class="nav">
        <ul>
            <li class="current"><a href="#a">menu1</a></li>
            <li><a href="#b">menu2</a>
                <ul class="sub">
                    <li><a href="#ba">sub2a</a></li>
                    <li><a href="#bb">sub2b</a></li>
                    <li><a href="#bc">sub2c</a></li>
                </ul>
            </li>
            <li><a href="#c">menu3</a></li>
            <li><a href="#d">menu4</a>
                <ul class="sub">
                    <li><a href="#da">sub4a/a></li>
                        <li><a href="#db">sub4b</a></li>
                </ul>
            </li>
        </ul>
    </nav>

And here's the css:

 .nav li {
    list-style:none;
    line-height: 30px;  
 }

 .nav a:link, .nav a:visited{
    color:#333 !important;
    text-decoration:none;
 }

 .nav a:hover {
    color:#9bb710;
    text-decoration:none;
 }

 .current a:link, .current a:hover, .current a:visited {
    color:#9bb710 !important;
 }

 .sub {
    color:#333 !important;
    text-decoration:none;
    font-size: 14px;
    margin-left: 15px;
    line-height: 22px;
 }
2
  • Can you post your CSS Code too? Commented Jun 7, 2013 at 8:18
  • Hi Kishor, i've edited the first post and added the css. Commented Jun 7, 2013 at 8:21

3 Answers 3

2

This is happening due to event propagation: when you click on a subitem, "click" event is propagated up to the container "li".

You can prevent this by adding:

$("li").click(function(event) {
    event.stopPropagation();
});

Here's a jsfiddle example: http://jsfiddle.net/EWXPy/

Sign up to request clarification or add additional context in comments.

5 Comments

Is it possible to collapse the submenu when clicking on another main menu? Acutally it stays expanded.
Awesome - thank your very much!!!just one thing. Can I add a class "subcurrent" to the active submenu?
You mean like this? $("ul", this).addClass("subcurrent").toggle('slow');
Hi - close: I need it to the sub <li>-Element, like sub2a.
$(".subelement").click(function(){ $(".subelement").removeClass('subcurrent'); $(this).addClass("subcurrent"); - not sure if this is correct, but it works. If anyone has a better idea I'm thankfull. Thanks so far!
1

You forgot closing brackets in your script:

It has to be like this:

<script type="text/javascript">
        $(document).ready(function() {
         $('ul .sub').hide();
         $("li:has(.sub)").click(function() {
            $("ul", this).toggle('slow');
        });
});
</script>

Comments

0
$("li:has(.sub)").click(function(e) {
    if ($(e.target).parents('.sub').length) return false;

    $("ul", this).toggle('slow');
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.