We help IT Professionals succeed at work.
Get Started

jquery animate/toggle

a204801
a204801 asked
on
245 Views
Last Modified: 2015-07-07
I have recently been playing with jquery and wanted to find the best solution for an FAQ page.

with the code I have found, it works for 1 question, but if I had 30 questions on an FAQ page they would all animate up/down at the same time.  Whats the best way to edit this so I can have multiple questions that open individually onclick?  

What I have been able to piece together is the following:

<script type="text/javascript" src="//code.jquery.com/jquery-1.11.0.min.js"></script>
<script>
 $(document).ready(function() {
 
    $('.faq_question').click(function() {
 
        if ($(this).parent().is('.open')){
            $(this).closest('.faq').find('.faq_answer_container').animate({'height':'0'},500);
            $(this).closest('.faq').removeClass('open');
 
            }else{
                var newHeight =$(this).closest('.faq').find('.faq_answer').height() +'px';
                $(this).closest('.faq').find('.faq_answer_container').animate({'height':newHeight},500);
                $(this).closest('.faq').addClass('open');
            }
 
    });
 
});
</script>
<style>
/*FAQS*/

.faq_question {
	margin: 0px;
	padding: 0px 0px 5px 0px;
	display: inline-block;
	cursor: pointer;
	font-weight: bold;
}
.faq_answer_container {
	height: 0px;
	overflow: hidden;
	padding: 0px;
}
</style>

<div class="faq_container">
  <div class="faq">
    <div class="faq_question">Question goes here</div>
    <div class="faq_answer_container">
      <div class="faq_answer">Answer goes here</div>
    </div>
  </div>
</div>

Open in new window

Comment
Watch Question
CERTIFIED EXPERT
Expert of the Year 2008
Top Expert 2008
Commented:
This problem has been solved!
Unlock 1 Answer and 3 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE