HTML
Assuming the HTML is a alternating pattern of .question
and .answer
scheme.
<ul class="faq">
<li class="question">...</li>
<li class="answer">...</li>
...
</ul>
Narrow down the primary selector to .faq
. $("body")
and $(document)
should be used for certain events like "key"
or "load"
event types -- not common events like "click"
.
jQuery
The second parameter event.data
is used to designate this
(in this circumstance is also event.target
). In the example below .question
is this
:
$('.faq').on('click', '.question', function(event) {...
Reference $(this).next('.answer')
with a variable. A variable referencing a jQuery Object is commonly prefixed with a $
(recommended but not required).
let $answer = $(this).next('.answer');
The desired behavior is that of an accordion:
When an element among multiple alike element siblings is clicked (.question
that is this
or event.target
), will toggle open (and/or its associated element - $answer
) if it was originally closed and vice versa.
$answer.slideToggle(250).toggleClass('active');
$(this).toggleClass('active');
All sibling elements will close with the exception of this
(and associated elements if applicable). The exception can be selected by using the .not()
method.
$('.question').not(this).removeClass('active')
$('.answer').not($answer).slideUp(250).removeClass('active');
'shopify:block:select/deselect'
events are non-standard events that are unique to the Shopify platform. Not 100% sure if the following would work, but if they can be delegated with the .on()
method, then it could probably be triggered with the .trigger()
method.
if ($(this).hasClass('active')) {
$answer.trigger('shopify:block:select');
$('.answer').not($answer).trigger('shopify:block:deselect');
} else {
$('.answer').trigger('shopify:block:deselect');
}
Demo
$(function() {
$('.answer').hide();
$('.faq').on('click', '.question', function(e) {
let $answer = $(this).next('.answer');
$answer.slideToggle(250).toggleClass('active');
$(this).toggleClass('active');
$('.question').not(this).removeClass('active');
$('.answer').not($answer).slideUp(250).removeClass('active');
if ($(this).hasClass('active')) {
$answer.trigger('shopify:block:select');
$('.answer').not($answer).trigger('shopify:block:deselect');
} else {
$('.answer').trigger('shopify:block:deselect');
}
});
});
:root {
font: 400 3vw/6vh Arial
}
li {
padding: 3vh 2vw 1vh;
margin-bottom: 2vh
}
.question {
cursor: pointer
}
.answer {
list-style: none;
color: blue
}
.active {
text-decoration: underline;
font-weight: 900
}
<main>
<section>
<header>
<h1>FAQ</h1>
</header>
<ul class='faq'>
<li class='question'>Question?</li>
<li class='answer'>Answer.</li>
<li class='question'>Question?</li>
<li class='answer'>Answer.</li>
<li class='question'>Question?</li>
<li class='answer'>Answer.</li>
<li class='question'>Question?</li>
<li class='answer'>Answer.</li>
<li class='question'>Question?</li>
<li class='answer'>Answer.</li>
</ul>
</section>
</main>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>