bootstrap.js Accordion Collapse / Expand
I'm trying to create previous / next buttons on each accordion body.
I can't figure out a way to collapse / expand a certain section. I tried removing the class in
from the accordion-body
but that does not seem to collapse.
$(".accordion-body").each(function(){
if($(this).hasClass("in")) {
$(this).removeClass("in");
}
});
Also whenever or whatever I use the .collapse
method on, I get a javascript error saying that object has no method collapse.
javascript jquery twitter-bootstrap
add a comment |
I'm trying to create previous / next buttons on each accordion body.
I can't figure out a way to collapse / expand a certain section. I tried removing the class in
from the accordion-body
but that does not seem to collapse.
$(".accordion-body").each(function(){
if($(this).hasClass("in")) {
$(this).removeClass("in");
}
});
Also whenever or whatever I use the .collapse
method on, I get a javascript error saying that object has no method collapse.
javascript jquery twitter-bootstrap
@Nofel (editor) Please don't remove JavaScript from jQuery questions. jQuery is written in JavaScript and by removing the tag, you're removing a potential audience for these questions.
– Heretic Monkey
May 4 '17 at 18:39
add a comment |
I'm trying to create previous / next buttons on each accordion body.
I can't figure out a way to collapse / expand a certain section. I tried removing the class in
from the accordion-body
but that does not seem to collapse.
$(".accordion-body").each(function(){
if($(this).hasClass("in")) {
$(this).removeClass("in");
}
});
Also whenever or whatever I use the .collapse
method on, I get a javascript error saying that object has no method collapse.
javascript jquery twitter-bootstrap
I'm trying to create previous / next buttons on each accordion body.
I can't figure out a way to collapse / expand a certain section. I tried removing the class in
from the accordion-body
but that does not seem to collapse.
$(".accordion-body").each(function(){
if($(this).hasClass("in")) {
$(this).removeClass("in");
}
});
Also whenever or whatever I use the .collapse
method on, I get a javascript error saying that object has no method collapse.
javascript jquery twitter-bootstrap
javascript jquery twitter-bootstrap
asked Oct 2 '12 at 21:06
MichaelMichael
90641532
90641532
@Nofel (editor) Please don't remove JavaScript from jQuery questions. jQuery is written in JavaScript and by removing the tag, you're removing a potential audience for these questions.
– Heretic Monkey
May 4 '17 at 18:39
add a comment |
@Nofel (editor) Please don't remove JavaScript from jQuery questions. jQuery is written in JavaScript and by removing the tag, you're removing a potential audience for these questions.
– Heretic Monkey
May 4 '17 at 18:39
@Nofel (editor) Please don't remove JavaScript from jQuery questions. jQuery is written in JavaScript and by removing the tag, you're removing a potential audience for these questions.
– Heretic Monkey
May 4 '17 at 18:39
@Nofel (editor) Please don't remove JavaScript from jQuery questions. jQuery is written in JavaScript and by removing the tag, you're removing a potential audience for these questions.
– Heretic Monkey
May 4 '17 at 18:39
add a comment |
8 Answers
8
active
oldest
votes
The in
class is just an indicator that a section is open. The Javascript module applies the same inline styles as .in
does, so removing the class is insufficient.
You need to use the module's API to programmatically interact with the accordion, via the .collapse()
method:
$('.accordion-body').each(function(){
if ($(this).hasClass('in')) {
$(this).collapse('toggle');
}
});
Or, you can condense this down to:
$('.accordion-body.in').collapse('toggle');
If you want only to collapse any open sections:
$('.accordion-body').collapse('hide');
If you want only to expanded any closed sections:
$('.accordion-body').collapse('show');
2
Bootstrap documentation for the above answer: getbootstrap.com/javascript/#collapse
– Mike Richards
Sep 4 '15 at 14:08
1
I don't think this works in bootstrap 3..
– anthonygore
Dec 10 '15 at 5:50
9
Doesn't work. Once you call 'collapse' on an accordion item, it stops functioning as an accordion. For example, if you had another one open and you then open the one you called 'collapse' on, the other one no longer closes as expected. See: "Bootstrap Collapse doesn't toggle after you show, hide or toggle from code" stackoverflow.com/questions/17750907/…
– Triynko
Jan 19 '16 at 19:55
.collapse('show')
Works for me in v3.3.5. The docs say it requires collapse.js and the accordion plugin.
– kristianp
Jul 11 '18 at 1:03
add a comment |
Here is another solution:
/**
* Make an accordion active
* @param {String} id ID of the accordion
*/
var activateAccordion = function (id) {
// Get the parents
var parents = $('a[href="#' + id + '"]').parents('.panel-group').children('.panel');
// Go through each of the parents
$.each(parents, function (idx, obj) {
// Check if the child exists
var find = $(obj).find('a[href="#' + id + '"]'),
children = $(obj).children('.panel-collapse');
if (find.length > 0) {
// Show the selected child
children.removeClass('collapse');
children.addClass('in');
} else {
// Hide the others
children.removeClass('in');
children.addClass('collapse');
}
});
};
The important part of the code is the combination, remembering the .collapse class, not just using .in:
// Show the selected child
children.removeClass('collapse');
children.addClass('in');
and
// Hide the others
children.removeClass('in');
children.addClass('collapse');
The above example has been tested in Twitter's Bootstrap v3.3.4
This solution works great, thanks a lot!
– Tsung-Ting Kuo
Feb 25 '16 at 1:02
add a comment |
This works for accordions in Bootstrap 3:
var selector = $('.panel-heading a[data-toggle="collapse"]');
selector.on('click',function() {
var self = this;
if ($(this).hasClass('collapsed')) {
$.each(selector, function(key, value) {
if (!$(value).hasClass('collapsed') && value != self) {
$(value).trigger('click');
}
});
}
});
I'm simulating a click of the other accordion tabs with $(value).trigger('click');
. According to the API you should just be able to use the .collapse()
method i.e. $(value).collapse('hide');
. But it doesn't work for some reason so trigger
it is...
add a comment |
For this kind of problem use addClass("in"); only because of using ".collapse('toggle/Hide/Show');" will disturb the future toggle functionality.
add a comment |
Another related use case is when we have forms inside accordions & we want to expand accordion with validation errors. Extending the answer by Daniel Wright
https://stackoverflow.com/a/12698720/6504104, we can do something like
/**
* Expands accordions that have fields with errors
*
*/
var _expandAccordions = function () {
$form = $('.my-input-form');
// you can adjust the following lines to match your form structure / error classes
var $formGroups = $form.find('.form-group.has-error');
var $accordions = $formGroups.closest('.accordion-body');
$accordions.each(function () {
$(this).collapse('show');
});
};
add a comment |
I did,
$('.mb-0').click(function(){
$('.collapse').collapse('hide');
$('.collapse.in').collapse('show');
});
and this works for me
add a comment |
Using Bootstrap 4 add the following buttons inside the card body
<input type="button" class="btn btn-secondary btn-block btn-sm mt-3 text-center card-proceed-next" value="Proceed" />
<input type="button" class="btn btn-secondary btn-block btn-sm mt-3 text-center card-proceed-prev" value="Previous" />
Add the following javascript (includes Azhar Khattak show panels with validation errors):
$(function () {
$('.card-proceed-next').click(function (e) {
e.preventDefault();
$(e.target).closest('.collapse').collapse('hide'); // hide current accordion panel
$(e.target).closest('.card').next('.card').find('.collapse').addClass('show'); // show next accordion panel
});
$('.card-proceed-prev').click(function (e) {
e.preventDefault();
$(e.target).closest('.collapse').collapse('hide'); // hide current accordion panel
$(e.target).closest('.card').prev('.card').find('.collapse').addClass('show'); // show previous accordion panel
});
var $elErrors = $('#accordion').find('.field-validation-error'); // elements with error class
var $accordionsWithErrors = $elErrors.closest('.collapse'); // accordions with error elements
if ($accordionsWithErrors.length > 0) $('.collapse').collapse(); // collapse all accordion panels due to the first accordion panel shown as default
$accordionsWithErrors.each(function () {
$(this).addClass('show'); // show accordion panels with error messages
});
});
add a comment |
with vanilla javascript
const el = document.getElementById('bodyCollapse');
el.click();
where tagsCollapse
is id
of the original collapse trigger button. Something like -
<a
data-toggle="collapse"
href="#accordionBody"
role="button"
id="bodyCollapse"
aria-expanded="false"
aria-controls="accordionBody"
>
accordion header
</a>
You could wrap the script in a function that accepts one parameter (id) and invoke the function whenever you need to collapse an accordion.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f12698331%2fbootstrap-js-accordion-collapse-expand%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
8 Answers
8
active
oldest
votes
8 Answers
8
active
oldest
votes
active
oldest
votes
active
oldest
votes
The in
class is just an indicator that a section is open. The Javascript module applies the same inline styles as .in
does, so removing the class is insufficient.
You need to use the module's API to programmatically interact with the accordion, via the .collapse()
method:
$('.accordion-body').each(function(){
if ($(this).hasClass('in')) {
$(this).collapse('toggle');
}
});
Or, you can condense this down to:
$('.accordion-body.in').collapse('toggle');
If you want only to collapse any open sections:
$('.accordion-body').collapse('hide');
If you want only to expanded any closed sections:
$('.accordion-body').collapse('show');
2
Bootstrap documentation for the above answer: getbootstrap.com/javascript/#collapse
– Mike Richards
Sep 4 '15 at 14:08
1
I don't think this works in bootstrap 3..
– anthonygore
Dec 10 '15 at 5:50
9
Doesn't work. Once you call 'collapse' on an accordion item, it stops functioning as an accordion. For example, if you had another one open and you then open the one you called 'collapse' on, the other one no longer closes as expected. See: "Bootstrap Collapse doesn't toggle after you show, hide or toggle from code" stackoverflow.com/questions/17750907/…
– Triynko
Jan 19 '16 at 19:55
.collapse('show')
Works for me in v3.3.5. The docs say it requires collapse.js and the accordion plugin.
– kristianp
Jul 11 '18 at 1:03
add a comment |
The in
class is just an indicator that a section is open. The Javascript module applies the same inline styles as .in
does, so removing the class is insufficient.
You need to use the module's API to programmatically interact with the accordion, via the .collapse()
method:
$('.accordion-body').each(function(){
if ($(this).hasClass('in')) {
$(this).collapse('toggle');
}
});
Or, you can condense this down to:
$('.accordion-body.in').collapse('toggle');
If you want only to collapse any open sections:
$('.accordion-body').collapse('hide');
If you want only to expanded any closed sections:
$('.accordion-body').collapse('show');
2
Bootstrap documentation for the above answer: getbootstrap.com/javascript/#collapse
– Mike Richards
Sep 4 '15 at 14:08
1
I don't think this works in bootstrap 3..
– anthonygore
Dec 10 '15 at 5:50
9
Doesn't work. Once you call 'collapse' on an accordion item, it stops functioning as an accordion. For example, if you had another one open and you then open the one you called 'collapse' on, the other one no longer closes as expected. See: "Bootstrap Collapse doesn't toggle after you show, hide or toggle from code" stackoverflow.com/questions/17750907/…
– Triynko
Jan 19 '16 at 19:55
.collapse('show')
Works for me in v3.3.5. The docs say it requires collapse.js and the accordion plugin.
– kristianp
Jul 11 '18 at 1:03
add a comment |
The in
class is just an indicator that a section is open. The Javascript module applies the same inline styles as .in
does, so removing the class is insufficient.
You need to use the module's API to programmatically interact with the accordion, via the .collapse()
method:
$('.accordion-body').each(function(){
if ($(this).hasClass('in')) {
$(this).collapse('toggle');
}
});
Or, you can condense this down to:
$('.accordion-body.in').collapse('toggle');
If you want only to collapse any open sections:
$('.accordion-body').collapse('hide');
If you want only to expanded any closed sections:
$('.accordion-body').collapse('show');
The in
class is just an indicator that a section is open. The Javascript module applies the same inline styles as .in
does, so removing the class is insufficient.
You need to use the module's API to programmatically interact with the accordion, via the .collapse()
method:
$('.accordion-body').each(function(){
if ($(this).hasClass('in')) {
$(this).collapse('toggle');
}
});
Or, you can condense this down to:
$('.accordion-body.in').collapse('toggle');
If you want only to collapse any open sections:
$('.accordion-body').collapse('hide');
If you want only to expanded any closed sections:
$('.accordion-body').collapse('show');
answered Oct 2 '12 at 21:36
Daniel WrightDaniel Wright
4,22122227
4,22122227
2
Bootstrap documentation for the above answer: getbootstrap.com/javascript/#collapse
– Mike Richards
Sep 4 '15 at 14:08
1
I don't think this works in bootstrap 3..
– anthonygore
Dec 10 '15 at 5:50
9
Doesn't work. Once you call 'collapse' on an accordion item, it stops functioning as an accordion. For example, if you had another one open and you then open the one you called 'collapse' on, the other one no longer closes as expected. See: "Bootstrap Collapse doesn't toggle after you show, hide or toggle from code" stackoverflow.com/questions/17750907/…
– Triynko
Jan 19 '16 at 19:55
.collapse('show')
Works for me in v3.3.5. The docs say it requires collapse.js and the accordion plugin.
– kristianp
Jul 11 '18 at 1:03
add a comment |
2
Bootstrap documentation for the above answer: getbootstrap.com/javascript/#collapse
– Mike Richards
Sep 4 '15 at 14:08
1
I don't think this works in bootstrap 3..
– anthonygore
Dec 10 '15 at 5:50
9
Doesn't work. Once you call 'collapse' on an accordion item, it stops functioning as an accordion. For example, if you had another one open and you then open the one you called 'collapse' on, the other one no longer closes as expected. See: "Bootstrap Collapse doesn't toggle after you show, hide or toggle from code" stackoverflow.com/questions/17750907/…
– Triynko
Jan 19 '16 at 19:55
.collapse('show')
Works for me in v3.3.5. The docs say it requires collapse.js and the accordion plugin.
– kristianp
Jul 11 '18 at 1:03
2
2
Bootstrap documentation for the above answer: getbootstrap.com/javascript/#collapse
– Mike Richards
Sep 4 '15 at 14:08
Bootstrap documentation for the above answer: getbootstrap.com/javascript/#collapse
– Mike Richards
Sep 4 '15 at 14:08
1
1
I don't think this works in bootstrap 3..
– anthonygore
Dec 10 '15 at 5:50
I don't think this works in bootstrap 3..
– anthonygore
Dec 10 '15 at 5:50
9
9
Doesn't work. Once you call 'collapse' on an accordion item, it stops functioning as an accordion. For example, if you had another one open and you then open the one you called 'collapse' on, the other one no longer closes as expected. See: "Bootstrap Collapse doesn't toggle after you show, hide or toggle from code" stackoverflow.com/questions/17750907/…
– Triynko
Jan 19 '16 at 19:55
Doesn't work. Once you call 'collapse' on an accordion item, it stops functioning as an accordion. For example, if you had another one open and you then open the one you called 'collapse' on, the other one no longer closes as expected. See: "Bootstrap Collapse doesn't toggle after you show, hide or toggle from code" stackoverflow.com/questions/17750907/…
– Triynko
Jan 19 '16 at 19:55
.collapse('show')
Works for me in v3.3.5. The docs say it requires collapse.js and the accordion plugin.– kristianp
Jul 11 '18 at 1:03
.collapse('show')
Works for me in v3.3.5. The docs say it requires collapse.js and the accordion plugin.– kristianp
Jul 11 '18 at 1:03
add a comment |
Here is another solution:
/**
* Make an accordion active
* @param {String} id ID of the accordion
*/
var activateAccordion = function (id) {
// Get the parents
var parents = $('a[href="#' + id + '"]').parents('.panel-group').children('.panel');
// Go through each of the parents
$.each(parents, function (idx, obj) {
// Check if the child exists
var find = $(obj).find('a[href="#' + id + '"]'),
children = $(obj).children('.panel-collapse');
if (find.length > 0) {
// Show the selected child
children.removeClass('collapse');
children.addClass('in');
} else {
// Hide the others
children.removeClass('in');
children.addClass('collapse');
}
});
};
The important part of the code is the combination, remembering the .collapse class, not just using .in:
// Show the selected child
children.removeClass('collapse');
children.addClass('in');
and
// Hide the others
children.removeClass('in');
children.addClass('collapse');
The above example has been tested in Twitter's Bootstrap v3.3.4
This solution works great, thanks a lot!
– Tsung-Ting Kuo
Feb 25 '16 at 1:02
add a comment |
Here is another solution:
/**
* Make an accordion active
* @param {String} id ID of the accordion
*/
var activateAccordion = function (id) {
// Get the parents
var parents = $('a[href="#' + id + '"]').parents('.panel-group').children('.panel');
// Go through each of the parents
$.each(parents, function (idx, obj) {
// Check if the child exists
var find = $(obj).find('a[href="#' + id + '"]'),
children = $(obj).children('.panel-collapse');
if (find.length > 0) {
// Show the selected child
children.removeClass('collapse');
children.addClass('in');
} else {
// Hide the others
children.removeClass('in');
children.addClass('collapse');
}
});
};
The important part of the code is the combination, remembering the .collapse class, not just using .in:
// Show the selected child
children.removeClass('collapse');
children.addClass('in');
and
// Hide the others
children.removeClass('in');
children.addClass('collapse');
The above example has been tested in Twitter's Bootstrap v3.3.4
This solution works great, thanks a lot!
– Tsung-Ting Kuo
Feb 25 '16 at 1:02
add a comment |
Here is another solution:
/**
* Make an accordion active
* @param {String} id ID of the accordion
*/
var activateAccordion = function (id) {
// Get the parents
var parents = $('a[href="#' + id + '"]').parents('.panel-group').children('.panel');
// Go through each of the parents
$.each(parents, function (idx, obj) {
// Check if the child exists
var find = $(obj).find('a[href="#' + id + '"]'),
children = $(obj).children('.panel-collapse');
if (find.length > 0) {
// Show the selected child
children.removeClass('collapse');
children.addClass('in');
} else {
// Hide the others
children.removeClass('in');
children.addClass('collapse');
}
});
};
The important part of the code is the combination, remembering the .collapse class, not just using .in:
// Show the selected child
children.removeClass('collapse');
children.addClass('in');
and
// Hide the others
children.removeClass('in');
children.addClass('collapse');
The above example has been tested in Twitter's Bootstrap v3.3.4
Here is another solution:
/**
* Make an accordion active
* @param {String} id ID of the accordion
*/
var activateAccordion = function (id) {
// Get the parents
var parents = $('a[href="#' + id + '"]').parents('.panel-group').children('.panel');
// Go through each of the parents
$.each(parents, function (idx, obj) {
// Check if the child exists
var find = $(obj).find('a[href="#' + id + '"]'),
children = $(obj).children('.panel-collapse');
if (find.length > 0) {
// Show the selected child
children.removeClass('collapse');
children.addClass('in');
} else {
// Hide the others
children.removeClass('in');
children.addClass('collapse');
}
});
};
The important part of the code is the combination, remembering the .collapse class, not just using .in:
// Show the selected child
children.removeClass('collapse');
children.addClass('in');
and
// Hide the others
children.removeClass('in');
children.addClass('collapse');
The above example has been tested in Twitter's Bootstrap v3.3.4
edited Sep 24 '15 at 10:18
answered Sep 24 '15 at 7:46
Cristi DraghiciCristi Draghici
43458
43458
This solution works great, thanks a lot!
– Tsung-Ting Kuo
Feb 25 '16 at 1:02
add a comment |
This solution works great, thanks a lot!
– Tsung-Ting Kuo
Feb 25 '16 at 1:02
This solution works great, thanks a lot!
– Tsung-Ting Kuo
Feb 25 '16 at 1:02
This solution works great, thanks a lot!
– Tsung-Ting Kuo
Feb 25 '16 at 1:02
add a comment |
This works for accordions in Bootstrap 3:
var selector = $('.panel-heading a[data-toggle="collapse"]');
selector.on('click',function() {
var self = this;
if ($(this).hasClass('collapsed')) {
$.each(selector, function(key, value) {
if (!$(value).hasClass('collapsed') && value != self) {
$(value).trigger('click');
}
});
}
});
I'm simulating a click of the other accordion tabs with $(value).trigger('click');
. According to the API you should just be able to use the .collapse()
method i.e. $(value).collapse('hide');
. But it doesn't work for some reason so trigger
it is...
add a comment |
This works for accordions in Bootstrap 3:
var selector = $('.panel-heading a[data-toggle="collapse"]');
selector.on('click',function() {
var self = this;
if ($(this).hasClass('collapsed')) {
$.each(selector, function(key, value) {
if (!$(value).hasClass('collapsed') && value != self) {
$(value).trigger('click');
}
});
}
});
I'm simulating a click of the other accordion tabs with $(value).trigger('click');
. According to the API you should just be able to use the .collapse()
method i.e. $(value).collapse('hide');
. But it doesn't work for some reason so trigger
it is...
add a comment |
This works for accordions in Bootstrap 3:
var selector = $('.panel-heading a[data-toggle="collapse"]');
selector.on('click',function() {
var self = this;
if ($(this).hasClass('collapsed')) {
$.each(selector, function(key, value) {
if (!$(value).hasClass('collapsed') && value != self) {
$(value).trigger('click');
}
});
}
});
I'm simulating a click of the other accordion tabs with $(value).trigger('click');
. According to the API you should just be able to use the .collapse()
method i.e. $(value).collapse('hide');
. But it doesn't work for some reason so trigger
it is...
This works for accordions in Bootstrap 3:
var selector = $('.panel-heading a[data-toggle="collapse"]');
selector.on('click',function() {
var self = this;
if ($(this).hasClass('collapsed')) {
$.each(selector, function(key, value) {
if (!$(value).hasClass('collapsed') && value != self) {
$(value).trigger('click');
}
});
}
});
I'm simulating a click of the other accordion tabs with $(value).trigger('click');
. According to the API you should just be able to use the .collapse()
method i.e. $(value).collapse('hide');
. But it doesn't work for some reason so trigger
it is...
answered Dec 10 '15 at 6:05
anthonygoreanthonygore
2,8262126
2,8262126
add a comment |
add a comment |
For this kind of problem use addClass("in"); only because of using ".collapse('toggle/Hide/Show');" will disturb the future toggle functionality.
add a comment |
For this kind of problem use addClass("in"); only because of using ".collapse('toggle/Hide/Show');" will disturb the future toggle functionality.
add a comment |
For this kind of problem use addClass("in"); only because of using ".collapse('toggle/Hide/Show');" will disturb the future toggle functionality.
For this kind of problem use addClass("in"); only because of using ".collapse('toggle/Hide/Show');" will disturb the future toggle functionality.
answered Sep 23 '16 at 14:46
VikramVikram
695
695
add a comment |
add a comment |
Another related use case is when we have forms inside accordions & we want to expand accordion with validation errors. Extending the answer by Daniel Wright
https://stackoverflow.com/a/12698720/6504104, we can do something like
/**
* Expands accordions that have fields with errors
*
*/
var _expandAccordions = function () {
$form = $('.my-input-form');
// you can adjust the following lines to match your form structure / error classes
var $formGroups = $form.find('.form-group.has-error');
var $accordions = $formGroups.closest('.accordion-body');
$accordions.each(function () {
$(this).collapse('show');
});
};
add a comment |
Another related use case is when we have forms inside accordions & we want to expand accordion with validation errors. Extending the answer by Daniel Wright
https://stackoverflow.com/a/12698720/6504104, we can do something like
/**
* Expands accordions that have fields with errors
*
*/
var _expandAccordions = function () {
$form = $('.my-input-form');
// you can adjust the following lines to match your form structure / error classes
var $formGroups = $form.find('.form-group.has-error');
var $accordions = $formGroups.closest('.accordion-body');
$accordions.each(function () {
$(this).collapse('show');
});
};
add a comment |
Another related use case is when we have forms inside accordions & we want to expand accordion with validation errors. Extending the answer by Daniel Wright
https://stackoverflow.com/a/12698720/6504104, we can do something like
/**
* Expands accordions that have fields with errors
*
*/
var _expandAccordions = function () {
$form = $('.my-input-form');
// you can adjust the following lines to match your form structure / error classes
var $formGroups = $form.find('.form-group.has-error');
var $accordions = $formGroups.closest('.accordion-body');
$accordions.each(function () {
$(this).collapse('show');
});
};
Another related use case is when we have forms inside accordions & we want to expand accordion with validation errors. Extending the answer by Daniel Wright
https://stackoverflow.com/a/12698720/6504104, we can do something like
/**
* Expands accordions that have fields with errors
*
*/
var _expandAccordions = function () {
$form = $('.my-input-form');
// you can adjust the following lines to match your form structure / error classes
var $formGroups = $form.find('.form-group.has-error');
var $accordions = $formGroups.closest('.accordion-body');
$accordions.each(function () {
$(this).collapse('show');
});
};
edited May 11 '18 at 18:05
answered May 11 '18 at 17:48
Azhar KhattakAzhar Khattak
35028
35028
add a comment |
add a comment |
I did,
$('.mb-0').click(function(){
$('.collapse').collapse('hide');
$('.collapse.in').collapse('show');
});
and this works for me
add a comment |
I did,
$('.mb-0').click(function(){
$('.collapse').collapse('hide');
$('.collapse.in').collapse('show');
});
and this works for me
add a comment |
I did,
$('.mb-0').click(function(){
$('.collapse').collapse('hide');
$('.collapse.in').collapse('show');
});
and this works for me
I did,
$('.mb-0').click(function(){
$('.collapse').collapse('hide');
$('.collapse.in').collapse('show');
});
and this works for me
edited Jun 24 '18 at 19:07
Failed Scientist
1,48932034
1,48932034
answered Jun 24 '18 at 17:37
Raphael OliveiraRaphael Oliveira
1
1
add a comment |
add a comment |
Using Bootstrap 4 add the following buttons inside the card body
<input type="button" class="btn btn-secondary btn-block btn-sm mt-3 text-center card-proceed-next" value="Proceed" />
<input type="button" class="btn btn-secondary btn-block btn-sm mt-3 text-center card-proceed-prev" value="Previous" />
Add the following javascript (includes Azhar Khattak show panels with validation errors):
$(function () {
$('.card-proceed-next').click(function (e) {
e.preventDefault();
$(e.target).closest('.collapse').collapse('hide'); // hide current accordion panel
$(e.target).closest('.card').next('.card').find('.collapse').addClass('show'); // show next accordion panel
});
$('.card-proceed-prev').click(function (e) {
e.preventDefault();
$(e.target).closest('.collapse').collapse('hide'); // hide current accordion panel
$(e.target).closest('.card').prev('.card').find('.collapse').addClass('show'); // show previous accordion panel
});
var $elErrors = $('#accordion').find('.field-validation-error'); // elements with error class
var $accordionsWithErrors = $elErrors.closest('.collapse'); // accordions with error elements
if ($accordionsWithErrors.length > 0) $('.collapse').collapse(); // collapse all accordion panels due to the first accordion panel shown as default
$accordionsWithErrors.each(function () {
$(this).addClass('show'); // show accordion panels with error messages
});
});
add a comment |
Using Bootstrap 4 add the following buttons inside the card body
<input type="button" class="btn btn-secondary btn-block btn-sm mt-3 text-center card-proceed-next" value="Proceed" />
<input type="button" class="btn btn-secondary btn-block btn-sm mt-3 text-center card-proceed-prev" value="Previous" />
Add the following javascript (includes Azhar Khattak show panels with validation errors):
$(function () {
$('.card-proceed-next').click(function (e) {
e.preventDefault();
$(e.target).closest('.collapse').collapse('hide'); // hide current accordion panel
$(e.target).closest('.card').next('.card').find('.collapse').addClass('show'); // show next accordion panel
});
$('.card-proceed-prev').click(function (e) {
e.preventDefault();
$(e.target).closest('.collapse').collapse('hide'); // hide current accordion panel
$(e.target).closest('.card').prev('.card').find('.collapse').addClass('show'); // show previous accordion panel
});
var $elErrors = $('#accordion').find('.field-validation-error'); // elements with error class
var $accordionsWithErrors = $elErrors.closest('.collapse'); // accordions with error elements
if ($accordionsWithErrors.length > 0) $('.collapse').collapse(); // collapse all accordion panels due to the first accordion panel shown as default
$accordionsWithErrors.each(function () {
$(this).addClass('show'); // show accordion panels with error messages
});
});
add a comment |
Using Bootstrap 4 add the following buttons inside the card body
<input type="button" class="btn btn-secondary btn-block btn-sm mt-3 text-center card-proceed-next" value="Proceed" />
<input type="button" class="btn btn-secondary btn-block btn-sm mt-3 text-center card-proceed-prev" value="Previous" />
Add the following javascript (includes Azhar Khattak show panels with validation errors):
$(function () {
$('.card-proceed-next').click(function (e) {
e.preventDefault();
$(e.target).closest('.collapse').collapse('hide'); // hide current accordion panel
$(e.target).closest('.card').next('.card').find('.collapse').addClass('show'); // show next accordion panel
});
$('.card-proceed-prev').click(function (e) {
e.preventDefault();
$(e.target).closest('.collapse').collapse('hide'); // hide current accordion panel
$(e.target).closest('.card').prev('.card').find('.collapse').addClass('show'); // show previous accordion panel
});
var $elErrors = $('#accordion').find('.field-validation-error'); // elements with error class
var $accordionsWithErrors = $elErrors.closest('.collapse'); // accordions with error elements
if ($accordionsWithErrors.length > 0) $('.collapse').collapse(); // collapse all accordion panels due to the first accordion panel shown as default
$accordionsWithErrors.each(function () {
$(this).addClass('show'); // show accordion panels with error messages
});
});
Using Bootstrap 4 add the following buttons inside the card body
<input type="button" class="btn btn-secondary btn-block btn-sm mt-3 text-center card-proceed-next" value="Proceed" />
<input type="button" class="btn btn-secondary btn-block btn-sm mt-3 text-center card-proceed-prev" value="Previous" />
Add the following javascript (includes Azhar Khattak show panels with validation errors):
$(function () {
$('.card-proceed-next').click(function (e) {
e.preventDefault();
$(e.target).closest('.collapse').collapse('hide'); // hide current accordion panel
$(e.target).closest('.card').next('.card').find('.collapse').addClass('show'); // show next accordion panel
});
$('.card-proceed-prev').click(function (e) {
e.preventDefault();
$(e.target).closest('.collapse').collapse('hide'); // hide current accordion panel
$(e.target).closest('.card').prev('.card').find('.collapse').addClass('show'); // show previous accordion panel
});
var $elErrors = $('#accordion').find('.field-validation-error'); // elements with error class
var $accordionsWithErrors = $elErrors.closest('.collapse'); // accordions with error elements
if ($accordionsWithErrors.length > 0) $('.collapse').collapse(); // collapse all accordion panels due to the first accordion panel shown as default
$accordionsWithErrors.each(function () {
$(this).addClass('show'); // show accordion panels with error messages
});
});
answered Nov 20 '18 at 10:31
JoddaJodda
244
244
add a comment |
add a comment |
with vanilla javascript
const el = document.getElementById('bodyCollapse');
el.click();
where tagsCollapse
is id
of the original collapse trigger button. Something like -
<a
data-toggle="collapse"
href="#accordionBody"
role="button"
id="bodyCollapse"
aria-expanded="false"
aria-controls="accordionBody"
>
accordion header
</a>
You could wrap the script in a function that accepts one parameter (id) and invoke the function whenever you need to collapse an accordion.
add a comment |
with vanilla javascript
const el = document.getElementById('bodyCollapse');
el.click();
where tagsCollapse
is id
of the original collapse trigger button. Something like -
<a
data-toggle="collapse"
href="#accordionBody"
role="button"
id="bodyCollapse"
aria-expanded="false"
aria-controls="accordionBody"
>
accordion header
</a>
You could wrap the script in a function that accepts one parameter (id) and invoke the function whenever you need to collapse an accordion.
add a comment |
with vanilla javascript
const el = document.getElementById('bodyCollapse');
el.click();
where tagsCollapse
is id
of the original collapse trigger button. Something like -
<a
data-toggle="collapse"
href="#accordionBody"
role="button"
id="bodyCollapse"
aria-expanded="false"
aria-controls="accordionBody"
>
accordion header
</a>
You could wrap the script in a function that accepts one parameter (id) and invoke the function whenever you need to collapse an accordion.
with vanilla javascript
const el = document.getElementById('bodyCollapse');
el.click();
where tagsCollapse
is id
of the original collapse trigger button. Something like -
<a
data-toggle="collapse"
href="#accordionBody"
role="button"
id="bodyCollapse"
aria-expanded="false"
aria-controls="accordionBody"
>
accordion header
</a>
You could wrap the script in a function that accepts one parameter (id) and invoke the function whenever you need to collapse an accordion.
answered Feb 6 at 13:11
Emmanuel NdukweEmmanuel Ndukwe
714
714
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f12698331%2fbootstrap-js-accordion-collapse-expand%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
@Nofel (editor) Please don't remove JavaScript from jQuery questions. jQuery is written in JavaScript and by removing the tag, you're removing a potential audience for these questions.
– Heretic Monkey
May 4 '17 at 18:39