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

Dropdown data-toggle="dropdown" element click events not bubble #30267

Closed
njake opened this issue Feb 22, 2020 · 11 comments · Fixed by #33442
Closed

Dropdown data-toggle="dropdown" element click events not bubble #30267

njake opened this issue Feb 22, 2020 · 11 comments · Fixed by #33442

Comments

@njake
Copy link

njake commented Feb 22, 2020

_addEventListeners function has two functions to prevent default link actions.
event.preventDefault()
event.stopPropagation()

If link element is not used for toggle, disabling events are not nesessary at all?

Reason is i need attach events to all toggle elements to get click events wanted from toggle elements.
Better way is to attach single event listener to parent and use event.target, this is not working now.

Is it possible to add check if toggle element is link, then add those events something like:
if(this._element.nodeName == 'a'){ event.preventDefault(); event.stopPropagation(); }
or using data attribute to disable/enable bubling.

@MartijnCuppens
Copy link
Member

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

This is an automated reply

@njake
Copy link
Author

njake commented Feb 25, 2020

Here is demo code. codepen
When you click dropdown toggle button -> event fires one time only.
Another clicks to toggle button never give events to container div.

@XhmikosR
Copy link
Member

I wonder if this issue is present on v5 too?

@sijusamson
Copy link
Contributor

@XhmikosR In v5, no event is being fired when I click on dropdown. Codepen for reference. Well if we remove event.stopPropagation() from /js/src/dropdown.js will resolve this issue.
Well, I wonder why event.stopPropagation() was added in dropdown.

@XhmikosR
Copy link
Member

PRs welcome for both branches as usual, just make sure we have tests that catch the issue.

@MartijnCuppens
Copy link
Member

Well, I wonder why event.stopPropagation() was added in dropdown.

Didn't look into the code, but buttons which are added can trigger form submits and <a>'s will trigger page navigation. That's probably why that was added.

@patrickhlauke
Copy link
Member

was the stopPropagation added to cater for odd situations like form controls inside the dropdowns? I have vague memories of something along those lines from back in the day...

@caseyjhol
Copy link
Contributor

caseyjhol commented Jan 6, 2021

Well, I wonder why event.stopPropagation() was added in dropdown.

Didn't look into the code, but buttons which are added can trigger form submits and <a>'s will trigger page navigation. That's probably why that was added.

event.preventDefault() should be all that's needed for that use case.

@samihoda
Copy link

Any news on this fix?

@pchaganti
Copy link

Any chance the above PRs will get accepted?

thanks

@njake
Copy link
Author

njake commented Feb 18, 2021

Removing event.stopPropagation() looks fine to me.

Tested with raw change like this and toggle button not work correctly anymore.

$.fn.dropdown.Constructor.prototype._addEventListeners = function _addEventListeners() {
    var _this = this;
    $(this._element).on('click.bs.dropdown', function(event) {
        event.preventDefault();
        _this.toggle();
    });
};

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