Slide effects animate an element's height.
.slideDown()— slide a hidden element open.slideUp()— slide it closed.slideToggle()— open or close
Speed and callback work like other effects.
FAQ accordion idea
- Hide all answers at the start
- When a question is clicked,
slideToggle()its answer - Close the other answers with
slideUp()
$('.question').click(function () {
$(this).next('.answer').slideToggle();
$('.answer').not($(this).next()).slideUp();
});
.next() finds the element right after the clicked question.
Example
Example — FAQ accordion with slideToggle
HTML
<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
<style>
.question { background:#1f386b; color:#fff; padding:10px; margin-top:6px; cursor:pointer; border-radius:4px; }
.answer { display:none; padding:10px; border:1px solid #ddd; border-top:none; }
</style>
<div class="question">Are AgyanAdda tutorials free?</div>
<div class="answer">Yes, every tutorial is free to read.</div>
<div class="question">Can I run the examples?</div>
<div class="answer">Yes, click "Try it Now" under any example.</div>
<div class="question">Do you have live classes?</div>
<div class="answer">Yes, see the Online Training page.</div>
<script>
$(function () {
$('.question').click(function () {
const $answer = $(this).next('.answer');
$('.answer').not($answer).slideUp();
$answer.slideToggle();
});
});
</script>