Vor
Vor

Reputation: 35129

How to hide sub-menu when other menu clicked JQuery

I'm trying to figure out one thing, I have a one page website and want hide sub-menus under portfolio when other menu links cliked http://jsfiddle.net/kuuwj/15/

HTML

<ul id="navbar">
    <li><a href="#home" id="nav-home">Home</a></li>
    <li><a href="#portfolio" id="nav-portfolio">Portfolio</a>
        <div class="portfolio-apps">
         <section id="website">
            <span class="button">AAAAAAAAAAAAAAAAAAAAAA</span>
         </section>
         <section id="gterminal">
            <span class="button">BBBBBBBBBBBBBBBBBBBBBB</span>
         </section>
         <section>
            <span class="button">CCCCCCCCCCCCCCCCCCCCCC</span>
         </section>
        </div>
    </li>
    <li><a href="#about" id="nav-about">About Me</a></li>
    <li><a href="#contact" id="nav-contact">Contact</a></li>
   </ul>

JS

$(document).ready(function () {
    var portf_apps = $('.portfolio-apps');
    portf_apps.hide();
    $('#nav-portfolio').click(function() {
        portf_apps.show();
    });
      
});

Upvotes: 2

Views: 845

Answers (2)

blented
blented

Reputation: 2807

Change your Javascript to this:

$('#navbar > li > a').click(function(){
    portf_apps.hide();
});
$('#nav-portfolio').unbind('click').click(function() {
    portf_apps.show();
});   

Upvotes: 2

Explosion Pills
Explosion Pills

Reputation: 191749

Bind another click event to the other navbar elements before the portfolio showing one:

$("#navbar a").on('click', function () {
   $(".portfolio-apps").hide(); 
});

var portf_apps = $('.portfolio-apps');
...

This will cause the portf_apps method to trigger afterwards which will show its children even if it's clicked. I suggest updating this to work with parent-child relationships generally, though.

http://jsfiddle.net/jWujm/

Upvotes: 1

Related Questions