I found one very good example of Collapsible Content on Internet but it's unfinished.
<div class="container faq_wrapper">
<div class="row">
<div class="span10 offset1">
<p>
</p>
<div class="faq-all-actions">
<a class="faq-expand" onclick="jQuery('.answer-wrapper').css('display','block');">Expand All</a> | <a class="faq-collapse" onclick="jQuery('.answer-wrapper').css('display','none');">Collapse All</a></div>
</div>
</div>
<div class="row">
<div class="span10 offset1">
<div class="question-wrapper">
<div class="arrow">
</div>
<div class="big-q">
Q</div>
<div class="question">
<h6>Can I try the software before I buy it?</div></h6>
<div class="answer-wrapper">
<div class="big-a">
A</div>
<div class="answer">
Yes! Simply <a href="/trial">download a free trial</a> and you'll have instant access to all features for 30 days, absolutely free. We don't require your credit card details or any commitment.</div>
</div>
</div>
</div>
</div>
</div>
How I can expand or hide the answer from the example when I click on the row?
You can do it even without JS using only CSS. Just see this example:
.collapsible > .item {
display: block;
cursor: pointer;
margin: 5px;
}
.collapsible > .item > .row {
display: block;
background: #bbb;
padding: 5px;
}
.collapsible > .item > .content {
display: none;
background: #ddd;
padding: 5px;
}
.collapsible > .item > input[type=checkbox] {
display: none;
}
.collapsible > .item > input[type=checkbox]:checked + .content {
display: block;
}
<div class="collapsible">
<label class="item">
<span class="row">Row 1</span>
<input type="checkbox" />
<span class="content">Content 1</span>
</label>
<label class="item">
<span class="row">Row 2</span>
<input type="checkbox" />
<span class="content">Content 2</span>
</label>
<label class="item">
<span class="row">Row 3</span>
<input type="checkbox" />
<span class="content">Content 3</span>
</label>
</div>
Bind a click event to the row and hide/show the child using .find()
<style>
.answer-wrapper {
display: none;
}
</style>
$(document)
.on('click','.row',function(){
$(this).find('.answer-wrapper').slideToggle();
})
;
http://jsfiddle.net/7bz5au97/
If you bind the event to the document, you won't have to worry about the DOM changing due to ajax calls or dynamic content.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With