-
-
Notifications
You must be signed in to change notification settings - Fork 78.8k
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
Collapse.js v4 only toggles one target? #17643
Comments
Hi @thecristen! You appear to have posted a live example (http://s.bootply.com/render/1lBIRBwnA0), which is always a good first step. However, according to the HTML5 validator, your example has some validation errors, which might potentially be causing your issue:
You'll need to fix these errors and post a revised example before we can proceed further. (Please note that this is a fully automated comment.) |
@fat Is this intentional? |
I also ran into this problem. Here's another short example... |
Has anyone found any workarounds for this issue? |
v3 was selecting all targets where as v4 is selecting only the first target which explains the difference in behavior. As I started to refactor v4 to act like v3, the decision to only select the first target seems intentional. In v3, the
You could use the |
@maxbeatty - Maybe I'm misunderstanding, but it seems like this v3 example contradicts what you're saying. There are two divs and their toggle states are opposite one another. Any button click shows the hidden div and hides the visible div. I can achieve the same thing in v4 but I have to use javascript. It's just really nice to accomplish this without scripting. |
I don't have a dog in this fight. I could have misunderstood the code and intentions. I was mainly trying to help with v4 issues 🚂
Before any clicks, those two divs are plain HTML divs with CSS classes dictating their appearance.
If in v3 your second div is already instantiated for some reason or another, it behaves the same as v4 https://jsfiddle.net/bnz8sa2L/1/ My outsider opinion is that it's better to declare the behavior as you need to in v4 than to depend on assumptions about how it'll behave like in v3 |
I ran into the same issue. I'd like to toggle all targets and not the first one. This is the whole point in using CSS selectors... This is how I hacked a fix for it: --- a/public/js/bootstrap.js
+++ b/public/js/bootstrap.js
@@ -1284,7 +1284,10 @@ var Collapse = (function ($) {
var selector = '[data-toggle="collapse"][data-parent="' + this._config.parent + '"]';
$(parent).find(selector).each(function (i, element) {
- _this6._addAriaAndCollapsedClass(Collapse._getTargetFromElement(element), [element]);
+ var element = element;
+ Collapse._getTargetsFromElement(element).each(function() {
+ _this6._addAriaAndCollapsedClass(this, [element]);
+ });
});
return parent;
@@ -1305,10 +1308,10 @@ var Collapse = (function ($) {
// static
}], [{
- key: '_getTargetFromElement',
- value: function _getTargetFromElement(element) {
+ key: '_getTargetsFromElement',
+ value: function _getTargetsFromElement(element) {
var selector = Util.getSelectorFromElement(element);
- return selector ? $(selector)[0] : null;
+ return $(selector ? selector : []);
}
}, {
key: '_jQueryInterface',
@@ -1353,11 +1356,14 @@ var Collapse = (function ($) {
$(document).on(Event.CLICK_DATA_API, Selector.DATA_TOGGLE, function (event) {
event.preventDefault();
- var target = Collapse._getTargetFromElement(this);
- var data = $(target).data(DATA_KEY);
- var config = data ? 'toggle' : $(this).data();
-
- Collapse._jQueryInterface.call($(target), config);
+ var $this = $(this);
+ var targets = Collapse._getTargetsFromElement(this);
+ targets.each(function() {
+ var data = $(this).data(DATA_KEY);
+ var config = data ? 'toggle' : $this.data();
+
+ Collapse._jQueryInterface.call($(this), config);
+ })
});
/** The patch is against the final bootstrap.js file. |
I'd definitely welcome a PR to address this in our next alpha. Having multiple targets trigger the same collapse element would be awesome. |
X-ref #21692 |
http://www.bootply.com/1lBIRBwnA0
This example, using Bootstrap v3, properly shows the two hidden rows when clicking the first plus button.
The same markup using the v4 source only toggles the first row.
The text was updated successfully, but these errors were encountered: