Monday, June 3, 2024
99
rated 0 times [  102] [ 3]  / answers: 1 / hits: 21476  / 10 Years ago, fri, january 23, 2015, 12:00:00

Can the toggle functionality on Bootstrap collapse accordion be disabled only on larger resolutions?



The goal is to have the accordion collapsed on small resolutions with the option to toggle states, and expanded on large resolutions with no option to toggle states. What would be the best way to use Bootstrap built in functionality to achieve this?



I have made a Fiddle demo with what I have now. I'm not good with JS.



JSFiddle DEMO: http://jsfiddle.net/1crojp98/1/



HTML:



<div class=panel-group id=accordion role=tablist aria-multiselectable=true>

<div class=panel panel-default col-sm-6>
<div class=panel-heading role=tab id=headingOne>
<h4 class=panel-title text-center>
<a data-toggle=collapse data-parent=#accordion href=#collapseOne aria-expanded=true aria-controls=collapseOne>
Panel 1
</a>
</h4>
</div>
<div id=collapseOne class=panel-collapse collapse in role=tabpanel aria-labelledby=headingOne>
<div class=panel-body>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut tristique laoreet dui, id porttitor ipsum convallis vel. Integer turpis nisl, rhoncus sed hendrerit sit amet, adipiscing nec eros. Suspendisse potenti. Nam quis risus libero. Vestibulum et diam nisl, eget feugiat leo.</p>
</div>
</div>
</div>

<div class=panel panel-default col-sm-6>
<div class=panel-heading role=tab id=headingTwo>
<h4 class=panel-title text-center>
<a class=collapsed data-toggle=collapse data-parent=#accordion href=#collapseTwo aria-expanded=false aria-controls=collapseTwo>
Panel 2
</a>
</h4>
</div>
<div id=collapseTwo class=panel-collapse collapse in role=tabpanel aria-labelledby=headingTwo>
<div class=panel-body>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut tristique laoreet dui, id porttitor ipsum convallis vel. Integer turpis nisl, rhoncus sed hendrerit sit amet, adipiscing nec eros. Suspendisse potenti. Nam quis risus libero. Vestibulum et diam nisl, eget feugiat leo.</p>
</div>
</div>
</div>

</div>


JavaScript:



$(document).ready(function(){
if ($(window).width() <= 768){
$('.panel-collapse').removeClass('in');
}
});

$(window).resize(function(){
if ($(window).width() >= 768){
$('.panel-collapse').addClass('in');
}
if ($(window).width() <= 768){
$('.panel-collapse').removeClass('in');
}
});

More From » twitter-bootstrap

 Answers
18

That's possible. You should just stop the click event's propagation:



$('a[data-toggle=collapse]').click(function(e){
if ($(window).width() >= 768) {
e.stopPropagation();
}
});


I've updated your code on jsFiddle http://jsfiddle.net/1crojp98/2/



But this code will disable the possibility to both collapse and open panels (only for larger resolutions, but anyway).


[#68113] Wednesday, January 21, 2015, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
daja

Total Points: 407
Total Questions: 103
Total Answers: 103

Location: Ghana
Member since Sun, Mar 27, 2022
2 Years ago
daja questions
Tue, Dec 21, 21, 00:00, 3 Years ago
Thu, Apr 23, 20, 00:00, 4 Years ago
Fri, Sep 6, 19, 00:00, 5 Years ago
Tue, Jul 23, 19, 00:00, 5 Years ago
;