Rocco
Rocco

Reputation: 5

Bootstrap Dropdown Keeps Collapsing on Mobile

Site: http://pegasusbus.com/ebrochure

My menu works great, but when i click on the dropdown for "packages" in the nav, the dropdown closes before i can select an option. This only happen on mobile.

Can anyone please tell me whats going on here please?

Upvotes: 0

Views: 1981

Answers (2)

vanburen
vanburen

Reputation: 21663

Inside your freelancer.js file you're targeting all the a tags inside the navbar (which includes the navbar-toggle) to collapse the mobile menu when clicking any link.

Currently you have this:

// Closes the Responsive Menu on Menu Item Click
$('.navbar-collapse ul li a').click(function() {
    $('.navbar-toggle:visible').click();
});

You can prevent this by using jQuery :not() Selector

// Closes the Responsive Menu on Menu Item Click
$('.navbar-collapse ul li a:not(.dropdown-toggle)').click(function() {
    $('.navbar-toggle:visible').click();
});

** As a Sidenote you should consider running your site through a Validation process if you have not all ready: See validator.nu as an example.

Working Example:

// Highlight the top nav as scrolling occurs
$('body').scrollspy({
  target: '.navbar-fixed-top'
})

// Closes the Responsive Menu on Menu Item Click
$('.navbar-collapse ul li a:not(.dropdown-toggle)').click(function() {
  $('.navbar-toggle:visible').click();
});
body {
  position: relative;
}
section {
  padding: 50px;
  height: 500px;
}
section:nth-child(3) {
  background-color: lightgreen;
}
section:nth-child(4) {
  background-color: lightblue;
}
section:nth-child(5) {
  background-color: darkblue;
}
section:nth-child(6) {
  background-color: lightcoral;
}
section:nth-child(7) {
  background-color: darkmagenta;
}
section:nth-child(8) {
  background-color: lightskyblue;
}
section:nth-child(9) {
  background-color: lightgoldenrodyellow;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" />
<nav class="navbar navbar-default navbar-fixed-top" id="tophead">
  <div class="container-fluid">
    <div class="navbar-header">
      <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#myNavbar">
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
      <a class="navbar-brand page-scroll" href="#">Pegasus eBrochure</a>
    </div>
    <div class="collapse navbar-collapse" id="myNavbar">
      <ul class="nav navbar-nav navbar-right">
        <li class="active"><a href="#">Home</a>
        </li>
        <li class="dropdown"> <a class="dropdown-toggle" data-toggle="dropdown" role="button">Packages<span class="caret"></span></a>
          <ul class="dropdown-menu">
            <li><a href="#deals">Vacation Deals</a>
            </li>
            <li><a href="#sports">Sports</a>
            </li>
            <li><a href="#shopping">Shopping</a>
            </li>
            <li><a href="#holiday">Holiday</a>
            </li>
            <li><a href="#circuit">Circuits</a>
            </li>
          </ul>
        </li>
        <li><a href="#about">About</a>
        </li>
        <li><a href="#contact">Contact</a>
        </li>
        <li><a href="#"><span class="glyphicon glyphicon-user"></span> Sign Up</a>
        </li>
        <li><a href="#"><span class="glyphicon glyphicon-log-in"></span> Login</a>
        </li>
      </ul>
    </div>
  </div>
</nav>
<section id="deals">
  <h1>Vacation Deals</h1>
</section>
<section id="sports">
  <h1>Sports</h1>
</section>
<section id="shopping">
  <h1>Shopping</h1>
</section>
<section id="holiday">
  <h1>Holiday</h1>
</section>
<section id="circuit">
  <h1>Circuits</h1>
</section>
<section id="about">
  <h1>About</h1>
</section>
<section id="contact">
  <h1>Contact</h1>
</section>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>

Upvotes: 2

Anil
Anil

Reputation: 996

I saw your website in the below line href="#" is missing just add that it will fix your problem

<a class="dropdown-toggle" data-toggle="dropdown" role="button">Packages<span class="caret"></span></a>

Upvotes: 0

Related Questions