1

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');
}

3 Answers 3

2

From the bootstrap-docs:

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

Demo

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

1 Comment

Perfect empiric... Thank you somuch
1

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

1 Comment

@ urbz, Thanks for the Answer ... But in this case, if I click outside of dropdown, div also should close with Dropdown... which is not happening in your expample... Thanks for your time
0

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);
});

1 Comment

@ Amit, Thanks for your reply... if I click outside of dropdown, div also should close with Dropdown... like "empiric's" solution

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.