I wanted to create some sort of FAQ page using a nested accordion but it somewhat didn’t work. I wanted to search through the title and the body of each card. So far I can only search through the title and it‘s still not hiding the irrelevant results.
When I search “nostro three”, nothing shows up. Only when I type “what to do”, the first parent accordion disappears.
I wanted to search all the content and hide the irrelevant information not related to the search result.
$(document).ready(function() {
$("#searchfaq").on("keypress click input", function() {
var val = $(this).val();
if (val.length) {
$(".accordion .card").hide().filter(function() {
return $('.card-title', this).text().toLowerCase().indexOf(val.toLowerCase()) > -1;
}).show();
} else {
$(".accordion .card").show();
}
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.2.1/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.2.1/css/bootstrap.min.css" />
<!-- Search Bar -->
<div class="row gap-y">
<div class="offset-md-2 col-md-8 col-12">
<div class="form-group input-group">
<input id="searchfaq" type="text" class="form-control" placeholder="Search FAQ">
</div>
</div>
</div>
<!-- Accordion -->
<div class="row">
<div class="col-md-8 mx-auto">
<div class="accordion accordion-connected accordion-arrow-right" id="accordion-1">
<div class="card">
<h5 class="card-title">
<a data-toggle="collapse" href="#collapse-1-1">About</a>
</h5>
<div id="collapse-1-1" class="collapse" data-parent="#accordion-1">
<h6 class="card-title card-inside">
<a data-toggle="collapse" href="#collapse-1-1-1">What is you?</a>
</h6>
<div id="collapse-1-1-1" class="collapse" data-parent="#accordion-1-1">
<div class="card-body" style="padding-left: 30px">
Lorem Ipsum dolor sit amet one.
</div>
</div>
<h6 class="card-title card-inside">
<a data-toggle="collapse" href="#collapse-1-1-2">What is other you?</a>
</h6>
<div id="collapse-1-1-2" class="collapse" data-parent="#accordion-1-1">
<div class="card-body" style="padding-left: 30px">
Lorem Ipsum dolor sit amet two.
</div>
</div>
<h6 class="card-title card-inside">
<a data-toggle="collapse" href="#collapse-1-1-3">What are we??</a>
</h6>
<div id="collapse-1-1-3" class="collapse" data-parent="#accordion-1-1">
<div class="card-body" style="padding-left: 30px">
Lorem Ipsum dolor sit amet three.
</div>
</div>
<h6 class="card-title card-inside">
<a data-toggle="collapse" href="#collapse-1-1-4">What we do?</a>
</h6>
<div id="collapse-1-1-4" class="collapse" data-parent="#accordion-1-1">
<div class="card-body" style="padding-left: 30px">
Lorem Ipsum dolor sit amet four.
</div>
</div>
<h6 class="card-title card-inside">
<a data-toggle="collapse" href="#collapse-1-1-5">What you need?</a>
</h6>
<div id="collapse-1-1-5" class="collapse" data-parent="#accordion-1-1">
<div class="card-body" style="padding-left: 30px">
Lorem Ipsum dolor sit amet five.
</div>
</div>
</div>
</div>
<div class="card">
<h5 class="card-title" style="font-family: 'Raleway Bold'">
<a class="collapsed" data-toggle="collapse" href="#collapse-1-2">Details</a>
</h5>
<div id="collapse-1-2" class="collapse" data-parent="#accordion-1">
<h6 class="card-title card-inside">
<a data-toggle="collapse" href="#collapse-1-2-1">How to do ?</a>
</h6>
<div id="collapse-1-2-1" class="collapse" data-parent="#accordion-1-2">
<div class="card-body" style="padding-left: 30px">
nostro apeirian cu est one
</div>
</div>
<h6 class="card-title card-inside">
<a data-toggle="collapse" href="#collapse-1-2-2">How it is?</a>
</h6>
<div id="collapse-1-2-2" class="collapse" data-parent="#accordion-1-2">
<div class="card-body" style="padding-left: 30px">
nostro apeirian cu est two.
</div>
</div>
<h6 class="card-title card-inside">
<a data-toggle="collapse" href="#collapse-1-2-3">When to do?</a>
</h6>
<div id="collapse-1-2-3" class="collapse" data-parent="#accordion-1-2">
<div class="card-body" style="padding-left: 30px">
nostro apeirian cu est three.
</div>
</div>
<h6 class="card-title card-inside">
<a data-toggle="collapse" href="#collapse-1-2-4">Where to do?</a>
</h6>
<div id="collapse-1-2-4" class="collapse" data-parent="#accordion-1-2">
<div class="card-body" style="padding-left: 30px">
nostro apeirian cu est four.
</div>
</div>
</div>
</div>
</div>
</div>
</div>
2
Answers
adding this line of code inside the in: if(val.length)
and this inside else:
and wrapping the child accordion with another div
I’ve created some code that works as an accordion, allowing a user to open up accordions (and nested accordions).
Search functionality is also demonstrated, with automatically opening any accordions where the term is found, and their parents.
The code is fully commented, but let me know if it isn’t clear.
Thanks to SLaks for the function to find the element’s text without its childrens’ text (link).
Demonstration