I built a FAQ page with the option to hide and show the content underneath each question. I have a "Expand all" functionality that let the user display all the questions onclick. When a question is expanded it gets a class of "selected".
I am trying to change the "Expand All" status when all questions (LIs) are expanded.
How can I check that all the LI have the CLASS "selected" at the same time?
I use the EACH method to get the LIs and their CLASS.
Thanks in advance
Method getElementsByClassName() returns a set of DOM elements that have a certain class name. Here is a canonical example of how to use the returned list of nodes: var elements = document. getElementsByClassName("class-1"); for (var i = 0, len = elements.
getElementsByClassName() The getElementsByClassName method of Document interface returns an array-like object of all child elements which have all of the given class name(s).
You can probably count the list items with selected
class against all list items:
if ($("#questions li.selected").length == $("#questions li").length) {
// all list items are selected
}
#questions
is the element that contains your list and of course it might be different in your code, but you should get the idea.
$("li:not(.selected)").length
Would give you the number of <li>
s that do not have the 'selected' class. If this figure was zero you could run your logic.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With