Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Collapse row on mouse click

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>
                &nbsp;</p>
            <div class="faq-all-actions">
                <a class="faq-expand" onclick="jQuery('.answer-wrapper').css('display','block');">Expand All</a>&nbsp;&nbsp;|&nbsp;&nbsp;<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">
                    &nbsp;</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&#39;ll have instant access to all features for 30 days, absolutely free. We don&#39;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?

like image 264
Peter Penzov Avatar asked Sep 27 '22 19:09

Peter Penzov


2 Answers

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>
like image 106
Taras Romaniv Avatar answered Oct 06 '22 02:10

Taras Romaniv


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.

like image 30
zgr024 Avatar answered Oct 06 '22 02:10

zgr024