Reputation: 406
I have written a JQuery code to show hide the div. I am not able to just show one at a time. Like if i click on the another div the previous div gets collapsed. I could not fix this out and just spend a day on this. How do i make the previous div collapsed when i click on the new div ? also, i want to collapse the div itself when i click on it.
$(document).ready(function() {
$('.feature').on('click', function(e) {
e.preventDefault();
let $this = $(this);
if ($this.hasClass('collapsed')) {
$this.removeClass('collapsed');
$this.find('.feature__description').show(100);
} else {
$this.find('.feature__description').hide(100);
$this.addClass('collapsed');
}
});
});
body {
background-image: linear-gradient(90deg, #0084cb 0%, #0073b2 100%);
background-color: #0073b2;
}
.container {
max-width: 1140px;
margin: 0 auto;
}
.feature {
padding: 8px 15px;
}
.feature__title {
position: relative;
font-size: 18px;
font-weight: 700;
cursor: pointer;
}
.feature--card {
box-shadow: 0 35px 30px 0 rgba(0, 0, 0, .06);
border-radius: 4px;
background-color: rgba(255, 255, 255, 0.1);
margin-bottom: 0.5rem;
color: #fff;
}
.feature__icon {
width: 20px;
margin-right: 10px;
display: none;
}
.feature__content {
width: 100%;
}
.feature__icon img {
width: 20px;
height: 20px;
object-fit: contain;
}
.feature__description {
line-height: 30px;
display: none;
padding: 15px 15px 15px 70px;
position: relative;
}
.feature__description::before {
content: '';
display: inline-block;
position: absolute;
box-sizing: border-box;
border-left: 1px dashed rgba(255, 255, 255, 0.4);
height: calc(100% - 30px);
left: 55px;
top: 15px;
}
.feature.collapsed .feature__title::after {
content: '+';
border: 1px solid rgba(255, 255, 255, 0.4);
background-color: transparent;
color: white;
transition: 0.4s cubic-bezier(0.075, 0.82, 0.165, 1);
}
.feature__title::after {
content: '-';
border: 1px solid rgba(255, 255, 255, 0.4);
background-color: #fff;
color: #333;
position: absolute;
right: 0;
width: 26px;
height: 26px;
text-align: center;
transition: 0.4s cubic-bezier(0.075, 0.82, 0.165, 1);
line-height: 1.2;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="container">
<div class="feature feature--card d-flex collapsed">
<div class="feature__content">
<div class="feature__title">QuickBooks Integration</div>
<div class="feature__description">PortPro is integrated with QuickBooks allowing you to better manage invoices and apply all payments. This data automatically flows back into your QuickBooks account.</div>
</div>
</div>
<div class="feature feature--card d-flex collapsed">
<div class="feature__content">
<div class="feature__title">QuickBooks Integration</div>
<div class="feature__description">PortPro is integrated with QuickBooks allowing you to better manage invoices and apply all payments. This data automatically flows back into your QuickBooks account.</div>
</div>
</div>
<div class="feature feature--card d-flex collapsed">
<div class="feature__content">
<div class="feature__title">QuickBooks Integration</div>
<div class="feature__description">PortPro is integrated with QuickBooks allowing you to better manage invoices and apply all payments. This data automatically flows back into your QuickBooks account.</div>
</div>
</div>
</div>
Thanks a lot.
Upvotes: 0
Views: 361
Reputation: 9984
Just hide all of the options before showing your selected option:
// Mark all the the features with your collapsed class
$('.feature').addClass('collapsed');
// Hide them all
$('.feature .feature__description').hide(100);
Full example:
$(document).ready(function() {
$('.feature').on('click', function(e) {
e.preventDefault();
let $this = $(this);
if ($this.hasClass('collapsed')) {
// New code
$('.feature').addClass('collapsed');
$('.feature .feature__description').hide(100);
//
$this.removeClass('collapsed');
$this.find('.feature__description').show(100);
} else {
$this.find('.feature__description').hide(100);
$this.addClass('collapsed');
}
});
});
body {
background-image: linear-gradient(90deg, #0084cb 0%, #0073b2 100%);
background-color: #0073b2;
}
.container {
max-width: 1140px;
margin: 0 auto;
}
.feature {
padding: 8px 15px;
}
.feature__title {
position: relative;
font-size: 18px;
font-weight: 700;
cursor: pointer;
}
.feature--card {
box-shadow: 0 35px 30px 0 rgba(0, 0, 0, .06);
border-radius: 4px;
background-color: rgba(255, 255, 255, 0.1);
margin-bottom: 0.5rem;
color: #fff;
}
.feature__icon {
width: 20px;
margin-right: 10px;
display: none;
}
.feature__content {
width: 100%;
}
.feature__icon img {
width: 20px;
height: 20px;
object-fit: contain;
}
.feature__description {
line-height: 30px;
display: none;
padding: 15px 15px 15px 70px;
position: relative;
}
.feature__description::before {
content: '';
display: inline-block;
position: absolute;
box-sizing: border-box;
border-left: 1px dashed rgba(255, 255, 255, 0.4);
height: calc(100% - 30px);
left: 55px;
top: 15px;
}
.feature.collapsed .feature__title::after {
content: '+';
border: 1px solid rgba(255, 255, 255, 0.4);
background-color: transparent;
color: white;
transition: 0.4s cubic-bezier(0.075, 0.82, 0.165, 1);
}
.feature__title::after {
content: '-';
border: 1px solid rgba(255, 255, 255, 0.4);
background-color: #fff;
color: #333;
position: absolute;
right: 0;
width: 26px;
height: 26px;
text-align: center;
transition: 0.4s cubic-bezier(0.075, 0.82, 0.165, 1);
line-height: 1.2;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="container">
<div class="feature feature--card d-flex collapsed">
<div class="feature__content">
<div class="feature__title">QuickBooks Integration</div>
<div class="feature__description">PortPro is integrated with QuickBooks allowing you to better manage invoices and apply all payments. This data automatically flows back into your QuickBooks account.</div>
</div>
</div>
<div class="feature feature--card d-flex collapsed">
<div class="feature__content">
<div class="feature__title">QuickBooks Integration</div>
<div class="feature__description">PortPro is integrated with QuickBooks allowing you to better manage invoices and apply all payments. This data automatically flows back into your QuickBooks account.</div>
</div>
</div>
<div class="feature feature--card d-flex collapsed">
<div class="feature__content">
<div class="feature__title">QuickBooks Integration</div>
<div class="feature__description">PortPro is integrated with QuickBooks allowing you to better manage invoices and apply all payments. This data automatically flows back into your QuickBooks account.</div>
</div>
</div>
</div>
Upvotes: 2
Reputation: 1855
you just need to remove the class from all of them and hide the divs. that should do it.
$(document).ready(function() {
$('.feature').on('click', function(e) {
e.preventDefault();
let $this = $(this);
$('.feature').each(function(){
$(this).addClass("collapsed");
$(this).find('.feature__description').hide(100);
})
if ($this.hasClass('collapsed')) {
$this.removeClass('collapsed');
$this.find('.feature__description').show(100);
} else {
$this.find('.feature__description').hide(100);
$this.addClass('collapsed');
}
});
});
body {
background-image: linear-gradient(90deg, #0084cb 0%, #0073b2 100%);
background-color: #0073b2;
}
.container {
max-width: 1140px;
margin: 0 auto;
}
.feature {
padding: 8px 15px;
}
.feature__title {
position: relative;
font-size: 18px;
font-weight: 700;
cursor: pointer;
}
.feature--card {
box-shadow: 0 35px 30px 0 rgba(0, 0, 0, .06);
border-radius: 4px;
background-color: rgba(255, 255, 255, 0.1);
margin-bottom: 0.5rem;
color: #fff;
}
.feature__icon {
width: 20px;
margin-right: 10px;
display: none;
}
.feature__content {
width: 100%;
}
.feature__icon img {
width: 20px;
height: 20px;
object-fit: contain;
}
.feature__description {
line-height: 30px;
display: none;
padding: 15px 15px 15px 70px;
position: relative;
}
.feature__description::before {
content: '';
display: inline-block;
position: absolute;
box-sizing: border-box;
border-left: 1px dashed rgba(255, 255, 255, 0.4);
height: calc(100% - 30px);
left: 55px;
top: 15px;
}
.feature.collapsed .feature__title::after {
content: '+';
border: 1px solid rgba(255, 255, 255, 0.4);
background-color: transparent;
color: white;
transition: 0.4s cubic-bezier(0.075, 0.82, 0.165, 1);
}
.feature__title::after {
content: '-';
border: 1px solid rgba(255, 255, 255, 0.4);
background-color: #fff;
color: #333;
position: absolute;
right: 0;
width: 26px;
height: 26px;
text-align: center;
transition: 0.4s cubic-bezier(0.075, 0.82, 0.165, 1);
line-height: 1.2;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="container">
<div class="feature feature--card d-flex collapsed">
<div class="feature__content">
<div class="feature__title">QuickBooks Integration</div>
<div class="feature__description">PortPro is integrated with QuickBooks allowing you to better manage invoices and apply all payments. This data automatically flows back into your QuickBooks account.</div>
</div>
</div>
<div class="feature feature--card d-flex collapsed">
<div class="feature__content">
<div class="feature__title">QuickBooks Integration</div>
<div class="feature__description">PortPro is integrated with QuickBooks allowing you to better manage invoices and apply all payments. This data automatically flows back into your QuickBooks account.</div>
</div>
</div>
<div class="feature feature--card d-flex collapsed">
<div class="feature__content">
<div class="feature__title">QuickBooks Integration</div>
<div class="feature__description">PortPro is integrated with QuickBooks allowing you to better manage invoices and apply all payments. This data automatically flows back into your QuickBooks account.</div>
</div>
</div>
</div>
Upvotes: 1