I'm currently learning JavaScript/JQuery, but have an issue at work that I'm running into.
I've assigned a class of 'question' to an<a>tag, and 'answer' to a<div>. When a user clicks on the question, the answer will slide down. However, the problem I'm running into is that when they click on a <a href="#" class="question">, all of the <div class="answer">'s are displayed.
How can I make it so that only one .answer for it's parent .question is displayed when clicked?
Here is my HTML:
<li class="question"><a href="#">Question 1</a>
<div class="answer"><p>This is answer for question 1</p></div></li>
<li class="question"><a href="#">Question 2</a>
<div class="answer"><p>This is answer for question 2</p></div></li>
Here is my jquery:
<script>
jQuery(document).ready(function ($) {
$('div.answer').hide();
$('li.question').click(function() {
$('div.answer').slideDown('fast');
return false;
});
});
</script>
and the site is: http://topactioninvestments.com/faq/
Thanks!
<a>tag" - No you haven't, you've assigned the class to the containing<li>. Also, the<div>for the answer is redundant: why not use<p class="answer">? (Surely you don't need the<div>and the<p>?)