Maybe it’s because it’s Friday but I’m stuck on a simple problem. I’m building a FAQ page where a user clicks on a question, revealing the answer beneath it. I’m trying to get the next() function to just grab the next instance of an “answer” div but, despite not getting any JS errors, the hidden div isn’t showing.
Here’s the code:
.answer { display:none; }
$("a.question").click(function(){
$(this).next(".answer").toggle();
});
<a href="#" class="question">Queston 1</a>
<div class="answer">Answer 1</div>
<a href="#" class="question">Queston 2</a>
<div class="answer">Answer 2</div>
It works for me.
You probably have some other element between the question and the answer.
If so, change it to
Alternatively, you might be running your code in the
<head>tag, before the<body>is parsed.If so, move your
<script>below the question tags, or wrap it in$(function() { ... });.