Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

using .dropdown('toggle') twice in JavaScript doesn't return the dropdown to its original state #10486

Closed
evanp opened this issue Sep 5, 2013 · 8 comments

Comments

@evanp
Copy link

evanp commented Sep 5, 2013

If you use the JavaScript interface for dropdown -- .dropdown("toggle") -- multiple times, the "open" class isn't cleared, so the dropdown stays open.

https://gist.github.com/evanp/6456479

@cvrebert
Copy link
Collaborator

cvrebert commented Sep 7, 2013

For future reference, a JS Bin or JSFiddle is preferred.
Here's a JS Bin of the Gist: http://jsbin.com/osACabU/3/edit

@ghost
Copy link

ghost commented Oct 12, 2013

I'm working on a major US retail site and need this to work. Is there a workaround or patch? This bug is a month old and, while it's not critical (as in halting the browser), it's still fairly major as something very fundamental is not working.

@devinmcinnis
Copy link

There are two problems here.

The first is the dropdown usage documentation. It says that there are 2 ways to use the dropdown: data-attribute or JavaScript. The problem is, the JavaScript method relies on the attribute data-toggle="dropdown" for it to close the menu.

js/dropdown.js

  toggle = '[data-toggle="dropdown"]'

  ..

  function clearMenus() {
    $(backdrop).remove()
    $(toggle).each(function (e) {
      var $parent = getParent($(this))
      if (!$parent.hasClass('open')) return
      $parent.trigger(e = $.Event('hide.bs.dropdown'))
      if (e.isDefaultPrevented()) return
      $parent.removeClass('open').trigger('hidden.bs.dropdown')
    })
  }

The other problem is (most likely because it's not working) with the gist. You have 2 click events on the same menu item which, after you've clicked it, will now run twice. Once on the click and once on the toggle essentially opening and closing the dropdown.

Solution: For now, use the data-toggle attribute, remove your JavaScript, and it'll be fine.

@evanp
Copy link
Author

evanp commented Dec 15, 2013

@devinmcinnis No, actually, "toggle" doesn't toggle. The problem is here:

https://github.com/twbs/bootstrap/blob/master/js/dropdown.js#L42

and here

https://github.com/twbs/bootstrap/blob/master/js/dropdown.js#L53

Bootstrap only toggles the "open" class if it's already set. So sending the "toggle" message doesn't actually "toggle" the class, so the widget doesn't ever close -- it just stays open.

I think it'd be pretty easy to throw an else on that if to clear the class.

As an aside, I've personally worked around this problem by toggling the class manually.

@devinmcinnis
Copy link

@evanp Bootstrap toggles the "open" class if it's not set. It will remove the "open" class if it is set.

The quick fix for this would be to set toggle:

toggle = '[data-toggle=dropdown], .dropdown > a'

The problem would then be that every dropdown would be initialized on load and that's probably not desired (but I also don't know a use case where you wouldn't want a dropdown initialized).

@evanp
Copy link
Author

evanp commented Dec 15, 2013

Yokay. It's been a while since I submitted this issue; I'll go back in and review the exact cause of the problem. In the meanwhile, can you put together some sample code that successfully toggles a dropdown on and off via Javascript?

@devinmcinnis
Copy link

You can use the data-attribute and .stopImmediatePropagination()

http://jsbin.com/osACabU/28

@fat
Copy link
Member

fat commented Dec 24, 2013

added a note to the docs about the reliance on the data attribute, hope that helps – thanks @devinmcinnis for the support

@fat fat closed this as completed Dec 24, 2013
@mdo mdo mentioned this issue Dec 25, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants