javascript - Bootstrap: Closing others nav menus on collapse via JS -


i've been new @ bootstrap , js. currently, i've 2 navbar-collapse submenus. close others, when 1 opened.

found codesnippet:

$(function () {     $('.navbar-collapse').on('show.bs.collapse', function (e) {         $('.navbar-collapse').not(this).collapse('hide');     }); }); 

i placed in $(document).ready(function(){...}); . event fired on collapse event, seems close navbar-collapse elements. why? , how can archiv goals?

here demo

method 1

to collapse can use like, can collapse navs has class "in" , because opened collapsible have class "in" added. catch them using below script close those.

 $('#accordion .collapse').on('show.bs.collapse', function (e) {         $('#accordion .in').collapse('hide');     }); 

method 2

 <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">      <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>      <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>    <div class="panel-group" id="accordion" role="tablist" aria-multiselectable="true">      <div class="panel panel-info">          <div class="panel-heading " role="tab" id="headingone">              <h4 class="panel-title">                  <a role="button" data-toggle="collapse" data-parent="#accordion" href="#per" aria-expanded="true" aria-controls="collapseone">                      <span class="glyphicon glyphicon-minus"></span> <strong>link 1</strong>                  </a>              </h4>          </div>          <!--to make defualt open panel put class "in" (without quotes)-->          <div id="per" class="panel-collapse collapse in" role="tabpanel" aria-labelledby="headingone">              <div class="panel-body">                  link 1    link 1    link 1    link 1    link 1    link 1              </div>          </div>      </div>      <div class="panel panel-info">          <div class="panel-heading" role="tab" id="headingtwo">              <h4 class="panel-title">                  <a class="collapsed" role="button" data-toggle="collapse" data-parent="#accordion" href="#family" aria-expanded="false" aria-controls="collapsetwo">                      <span class="glyphicon glyphicon-plus"></span> <strong>link 2</strong>                  </a>              </h4>          </div>          <div id="family" class="panel-collapse collapse" role="tabpanel" aria-labelledby="headingtwo">              <div class="panel-body">                  link 2  link 2  link 2  link 2  link 2  link 2              </div>          </div>      </div>      <div class="panel panel-info">          <div class="panel-heading" role="tab" id="headingthree">              <h4 class="panel-title">                  <a class="collapsed" role="button" data-toggle="collapse" data-parent="#accordion" href="#social" aria-expanded="false" aria-controls="collapsethree">                      <span class="glyphicon glyphicon-plus"></span> <strong>link 3</strong>                  </a>              </h4>          </div>          <div id="social" class="panel-collapse collapse" role="tabpanel" aria-labelledby="headingthree">              <div class="panel-body">                  link 3  link 3  link 3  link 3 link 3   link 3  link 3              </div>          </div>      </div>  </div>


Comments

Popular posts from this blog

angular - Is it possible to get native element for formControl? -

unity3d - Rotate an object to face an opposite direction -

javascript - Why jQuery Select box change event is now working? -