Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if all items have the same class

Tags:

jquery

each

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

like image 968
John Avatar asked Sep 20 '11 12:09

John


People also ask

How to get all elements with same class in JavaScript?

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.

How to get all same class name in JavaScript?

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).


2 Answers

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.

like image 109
Xion Avatar answered Oct 02 '22 18:10

Xion


$("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.

like image 42
ipr101 Avatar answered Oct 02 '22 17:10

ipr101