Reddy
Reddy

Reputation: 1497

Bootstrap & jQuery - Toggle Div based on Bootstrap Dropdown Menu open or close

I want to show a div when Bootstrap Dropdown opens and hide the same when dropdown closes...


FIDDLE


HTML


<ul class="dropdown" id="flatmenu">
  <li data-toggle="dropdown" aria-expanded="true"><a href="#">Dropdown<span class="caret"></span></a></li>
  <div class="dropdown-menu fm-container" role="menu" aria-labelledby="dropdownMenu">
    Dropdown Content
  </div>
</ul>


<div class="show-me">Show Me</div>

CSS


body{width:400px;margin:50px auto;}
ul,li{list-style:none;margin:0;padding:0;}
.show-me{display:none;margin-top:200px;}

jQuery


if($('#flatmenu').hasClass('open')){
    $('.show-me').show('slow');
}else{
    $('.show-me').hide('slow');
}

Upvotes: 1

Views: 1515

Answers (3)

Amit
Amit

Reputation: 847

You can try this updated fiddle

Code:

$('#flatmenu').on('click',function(){
    $this = $(this);
    setTimeout(function(){
        if($this.hasClass('open')){
            $('.show-me').show('slow');
        }else{
            $('.show-me').hide('slow');
        }
    },100);
});

Upvotes: 0

urbz
urbz

Reputation: 2667

You could do .click() for the element that it's intended for and use the .toggle() effect for the div that is going to show/hide instead.

Code snippet:

$("#flatmenu").click(function () {
    $('.show-me').toggle('slow');
});

JsFiddle demo

Upvotes: 1

empiric
empiric

Reputation: 7878

From the bootstrap-docs:

$('#flatmenu').on('shown.bs.dropdown hidden.bs.dropdown', function () {
  $('.show-me').toggle('slow');
});

Demo

Upvotes: 2

Related Questions