Novis
Novis

Reputation: 660

How to handle show/hide of multiple dropdown menu

I have two dropdown nav menu. I wanna hide "expanded" dropdown menu (About) when I click on another dropdown menu (Products).

Here is my fiddle:

[https://jsfiddle.net/83gLyof6/2/]

TIA

Upvotes: 0

Views: 570

Answers (2)

Sooriya Dasanayake
Sooriya Dasanayake

Reputation: 1156

add this $(".dropdown-menu").slideUp("fast"); to dropdown click function.

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery Hide Dropdown on Click Outside</title>
<style type="text/css">
    ul{
        padding: 0;
        list-style: none;
        background: #f2f2f2;
    }
    ul li{
        display: inline-block;
        position: relative;
        line-height: 21px;
        text-align: left;
    }
    ul li a{
        display: block;
        padding: 8px 25px;
        color: #333;
        text-decoration: none;
    }
    ul li a:hover{
        color: #fff;
        background: #939393;
    }
    ul li ul.dropdown-menu{
        min-width: 100%; /* Set width of the dropdown */
        background: #f2f2f2;
        display: none;
        position: absolute;
        z-index: 999;
        left: 0;
    }
    ul li ul.dropdown-menu li{
        display: block;
    }
</style>
<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
<script type="text/javascript">
    $(document).ready(function(){
        // Show hide popover
        $(".dropdown").click(function(){
           if(!$(this).find(".dropdown-menu").is(":visible")){
     
            $(".dropdown-menu").slideUp("fast");
            $(this).find(".dropdown-menu").slideToggle("fast");
           }
           else{
            $(".dropdown-menu").slideUp("fast");
           }
        });
    });
    $(document).on("click", function(event){
        var $trigger = $(".dropdown");
        if($trigger !== event.target && !$trigger.has(event.target).length){
            $(".dropdown-menu").slideUp("fast");
             $(this).find(".dropdown-menu").hide;
        }            
    });
</script>
</head>
<body>
    <ul>
        <li><a href="#">Home</a></li>
       <li class="dropdown">
            <a href="#">about &#9662;</a>
            <ul class="dropdown-menu">
                <li><a href="#">Laptops</a></li>
                <li><a href="#">Monitors</a></li>
                <li><a href="#">Printers</a></li>
            </ul>
        </li>
        <li class="dropdown">
            <a href="#">Products &#9662;</a>
            <ul class="dropdown-menu">
                <li><a href="#">Laptops</a></li>
                <li><a href="#">Monitors</a></li>
                <li><a href="#">Printers</a></li>
            </ul>
        </li>
        <li><a href="#">Contact</a></li>
    </ul>
</body>
</html>

Upvotes: 3

dshukertjr
dshukertjr

Reputation: 18642

Quick and dirty way, but it gets the job done:

$(".dropdown").click(function(){
  $(".opened-dropdown-menu").slideUp("fast");
  $(this).find(".dropdown-menu").toggleClass("opened-dropdown-menu").slideToggle("fast");
});

Upvotes: 1

Related Questions