Skip to content

toggle doesn't close the dropdown if there are no [data-toggle="dropdown"] #26420

Closed

Description

@rpokrovskij

ver 4.1

I've created Dropdown in javascript

 $(this.dropDownMenu).dropdown({
                    placement: 'bottom-start',
                    flip: false,
                    reference: this.filterInput
                });

dropDownMenu <ul> element contains both dropdown dropdown-menu

first point: toggle method open it (btw dropdown class is vital there, without it plugin doesn't work - why?)

second point: toggle doesn't close it.

Through debugger I see this logic

           // bootstrap dropdown code
          var context = $$$1(toggles[i]).data(DATA_KEY);
          var relatedTarget = {
            relatedTarget: toggles[i]
          };

          if (!context) {
            continue; // here we enter
          }
         //  remove `show`  class will be never passed.

I expecect that Dropdown could be opened and closed manually (through JS) - then requrements to have toggles is excessive (the same is true for dropdown class requirement - you use it to find dropdown-menu but if dropdown-menu by itself is an jquery element - it still should work )

Activity

  1. Johann-S commented on Apr 30, 2018

    @Johann-S
    Member

    Bug reports must include a live demo of the problem. Per our contributing guidelines, please create a reduced test case via CodePen or JS Bin and report back with your link, Bootstrap version, and specific browser and OS details.

  2. added
    awaiting-reply
    on Apr 30, 2018
  3. rpokrovskij commented on Apr 30, 2018

    @rpokrovskij
    Author

    Johan, first of all I would like to know does it sounds as a bug: that data-toggle="dropdown" is required.
    May be this is "by design" and you do not bother about Dropdown menus dropdown-menu created, opened and closed from user's code without dropdown and data-toggle="dropdown" .

  4. Johann-S commented on Apr 30, 2018

    @Johann-S
    Member

    Yep data-toggle="dropdown" is a requirement for us, it's the only way to be sure there is only one dropdown open (see: https://github.com/twbs/bootstrap/blob/v4-dev/js/src/dropdown.js#L336)

  5. rpokrovskij commented on Apr 30, 2018

    @rpokrovskij
    Author

    Then this just should be added to documentation:

    dropdown-menu is not enough to use Dropdown plugin from JavaScript, there should be corresponded dropdown and data-toggle="dropdown" on the form (even if you do not really need them since you are.. using JavaScript .. means DOM events to toggle menu);
    We recommend use popper.js directly for such scenarious.

    BTW this doesn't work either

    <ul class="dropdown-menu dropdown"  data-toggle='dropdown' >
    <li class="dropdown-item">..</li>
    <li class="dropdown-item">..</li>
    </ul>
    
    $('.dropdown-menu').dropdown({
                        placement: 'bottom-start',
                        flip: false,
                        reference: this.filterInput
                    }); // will not work because "initialization" is stolen by bs on document handler which is attached to dropdown.
    

    P.S. I have tried to use bs dropdown as autosuggestion popup.
    https://dashboardcode.github.io/BsMultiSelect/

    image

  6. XhmikosR commented on Oct 12, 2018

    @XhmikosR
    Member

    @Johann-S: is this still an issue (docs update)?

  7. Johann-S commented on Oct 21, 2018

    @Johann-S
    Member

    yep we should update our docs

  8. added
    docs
    help-wanted
    v4
    and removed
    awaiting-reply
    on Oct 28, 2018
  9. chamnap commented on Jan 13, 2019

    @chamnap

    It happens to me the same as @rpokrovskij.

    When adding data-toggle='dropdown', the initialization is already done by the data api on the document level, https://github.com/twbs/bootstrap/blob/v4-dev/js/src/dropdown.js#L515. Therefore, we cannot manually initialized it.

    Without data-toggle='dropdown', the dropdown doesn't close itself when clicking outside.

    Any workarounds for now?

  10. mkleehammer commented on Jun 6, 2019

    @mkleehammer

    Same. To be clear, I think OP is looking for a way to have a manually controlled dropdown without the automatic data-api triggering it to open.

    In my case I'd like to display a dropdown under search input group, but I don't want the user toggling it open and closed while the search is still executing. I'll find a workaround, but it would be simpler if there was an alternative markup that would stop the built-in event handler. (I'll probably go adding the "disabled" class to the input group, which I see the dropdown looks for. Not sure where that rabbit trail leads, though.)

  11. nteetor commented on Nov 12, 2019

    @nteetor

    I ran into this issue trying to using a text input as a dropdown toggle. I want to allow the user to click around in and edit the input's text value without triggering the menu. Adding and removing the disabled class did solve some of the related problems, i.e. clicking in the input no longer triggers the menu. However, adding the disabled class prevents some of the dropdown keyboard interactions.

  12. mdo commented on Sep 19, 2020

    @mdo
    Member

    Docs state the requirement about the data attribute in a (hopefully obvious) callout. https://getbootstrap.com/docs/4.5/components/dropdowns/#via-javascript

    f3d0fee

    Beyond that, I don't think there are any planned changes to how dropdowns work here unless @Johann-S wants to revisit things.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

docshelp-wantedv4

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions