Overview:
I'm working on a Q&A page for a website. All the answers are initially hidden. I'm trying to animate the answers sliding out. There are little pictures of Q and A next to the questions and answers, respectively.
I want to move the Q to the left, have the answer slide down, and the "A" picture to fade in on first click of the question.
On the second click of the same question, I want it to hide. If another one is clicked, hide all and display the answer.
Code:
I can't get the code to work properly. I'm assuming there is something wrong with my DOM tree navigation but I'm not sure what.
HTML:
<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>
This is one question/answer combo. There are five total on the page and they all use the same classes as the one shown (they all look exactly the same except for message content).
JQuery:
<script>
$(document).ready(function(){
$(".faq-question").toggle(function(){
<!-- this slides up all siblings -->
$(this).siblings().slideUp();
<!-- animation -->
$(this).find('.letter-q').animate({left:"-=15"},"slow", function() {
$(this).closest('faq').find('faq-answer-container').slideDown('slow');
$(".letter-a").fadeIn("slow");
});
},function() {
<!-- slides up all siblings -->
$(this).siblings().slideUp();
<!-- animation -->
$('.letter-q').animate({left:"+=15"},"slow", function() {
$(this).closest('faq').find('faq-answer-container').slideUp('slow');
$(".letter-a").fadeOut("slow");
});
});
});
</script>
.letter-q and .letter-a are the classes designated to the pictures. For some reason, on first click, the Q fades out and on second and all subsequent clicks, all the other Q's start shifting to the right. The answer never slides down. I'm fairly new to JQuery but I've read the API material on everything I used. I'm not sure what's going wrong.
Any help would be appreciated!