Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Twitter Bootstrap (v2.2.1) dropdown links not working on mobile

I have a Twitter Bootstrap (v2.2.1) collapsable navbar and dropdown buttons. They work all chirpy and happy in my desktop browser and also if I resize down my desktop browser window down to mobile size they work happily.

However on a mobile, the dropdowns will open up when you click on the dropdown but clicking on any of the links within the dropdown just causes the dropdown to close without following the link.

This seems similar to this other SO question, but the solution there was to move the data-toggle="dropdown" to the place I have them already.

Here's the html on the page for the dropdown buttons:

<html>
<head>
<link rel="stylesheet" href="http://twitter.github.com/bootstrap/assets/css/bootstrap.css" />
</head>
<body>
<div class="btn-group">
    <a class="btn btn-primary" href="link1.php">
        Dropdown button
    </a>
    <a class="btn btn-primary dropdown-toggle" href="#" data-toggle="dropdown">
        <span class="caret"></span>
    </a>
    <ul class="dropdown-menu">
        <li class="nav-header">Recent</li>
        <li><a href="http://google.com">Google</a></li>
        <li><a href="http://bing.com">Bing</a></li>
    </ul>
</div>
<script type="text/javascript" src="http://twitter.github.com/bootstrap/assets/js/jquery.js"></script>
<script type="text/javascript" src="http://twitter.github.com/bootstrap/assets/js/bootstrap.js"></script>
<script type="text/javascript">
    /**
     * Twitter Bootstrap JQuery
     */
    //!function ($) {
        $(function () {
            $('.dropdown-toggle').dropdown();
        });
    //}
</script>
</body>
</html>

I tested it on an android mobile in the Mobile Opera, Mobile Firefox and default android browser.

enter image description here

like image 836
icc97 Avatar asked Jan 09 '13 18:01

icc97


People also ask

Why dropdown is not working in mobile view?

It's caused by an outdated library called FastClick designed to make click events easier on mobile browsers. FastClick came out in 2013, back when mobile traffic was less common and mobile browsers were more rudimentary. At the time, FastClick was a great way to improve usability issues on mobile.

Why isn't my bootstrap dropdown not working?

Why is my drop down menu not working in bootstrap? Solution : The dropdown should be toggled via data attributes or using javascript. In the above program, we have forgotten to add a data attribute so the dropdown is not working. So add data-bs-toggle="dropdown" to toggle the dropdown.


1 Answers

I've tracked it down to a bug in Bootstrap 2.2.1 the changelog for 2.2.2 states:

Dropdowns: Temporary fix added for dropdowns on mobile to prevent them from closing early.

If I swapped the bootstrap.js on my site from v2.2.1 to v2.2.2 then the buttons start working.

So now, if I view this code on jsFiddle using the latest v2.2.2 Bootstrap then it works.

<html>
<head>
<link rel="stylesheet" href="http://twitter.github.com/bootstrap/assets/css/bootstrap.css" />
</head>
<body>
<div class="container">
<div class="btn-group">
    <a class="btn btn-primary" href="link1.php">
        Dropdown button
    </a>
    <a class="btn btn-primary dropdown-toggle" href="#" data-toggle="dropdown">
        <span class="caret"></span>
    </a>
    <ul class="dropdown-menu">
        <li class="nav-header">Recent</li>
        <li><a href="http://google.com">Google</a></li>
        <li><a href="http://bing.com">Bing</a></li>
    </ul>
</div>
</div>
<script type="text/javascript" src="http://twitter.github.com/bootstrap/assets/js/jquery.js"></script>
<script type="text/javascript" src="http://twitter.github.com/bootstrap/assets/js/bootstrap.js"></script>
<script type="text/javascript">
        $(function () {
            $('.dropdown-toggle').dropdown();
        });
</script>   
</body>
</html>
like image 74
icc97 Avatar answered Sep 30 '22 06:09

icc97