Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check multiple elements has same class using jquery?

Is it possible to minify this selector?

if ($("#element1").hasClass("highlight") && $("#element2").hasClass("highlight") && $("#element3").hasClass("highlight") && $("#element4").hasClass("highlight") && $("#element5").hasClass("highlight")) {
  $("#newstyle).css("visibility", "visible");
};

I already tried

if ($("#element1, #element2, #element3, #element4, #element5").hasClass("highlight"))

and

if ($("#element1 && #element2 && #element3 && #element4 && #element5").hasClass("highlight"))

...but that's incorrect.

Basically I have many conditions the same like this: 5 specified elements hasClass XYZ. So I'd appreciate it to compress it.

like image 305
Sebastian Avatar asked Apr 22 '17 18:04

Sebastian


People also ask

Can we use same class on multiple elements in HTML?

Multiple HTML elements can share the same class.

Can you use the same class name on multiple elements?

The class attribute assigns a class name to the element, and that name can be used on many elements within the page.

Can I use multiple find in jQuery?

Use a comma to union multiple queries: var tds = $(this). find('input, textarea'); You can also use :input as a selector, but it's not as efficient and may also include some things you'd rather not include.

How do you loop through an element with the same class in jQuery?

Answer: Use the jQuery each() Method You can simply use the jQuery each() method to loop through elements with the same class and perform some action based on the specific condition.


2 Answers

You're on the right track. I should note that I'm assuming the pattern element1, element2, element3 isn't really how your IDs are, that you just used that in the question...

I'd probably turn it a bit on its head: Use a group of selectors with #element:not(.highlight) and if the result is empty (length == 0), they all either have it or don't exist.

if (!$("#element1:not(.highlight), #element2:not(.highlight), #element3:not(.highlight), #element4:not(.highlight), #element5:not(.highlight)").length) {

  $("#newstyle").css("visibility", "visible");

}

Example where they all have the class:

if (!$("#element1:not(.highlight), #element2:not(.highlight), #element3:not(.highlight), #element4:not(.highlight), #element5:not(.highlight)").length) {

  $("#newstyle").css("visibility", "visible");

}
<div id="element1" class="highlight"></div>
<div id="element2" class="highlight"></div>
<div id="element3" class="highlight"></div>
<div id="element4" class="highlight"></div>
<div id="element5" class="highlight"></div>
<div id="newstyle" style="visibility: hidden">I'm visible!</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

And where at least one of them doesn't:

Example where they all have the class:

if (!$("#element1:not(.highlight), #element2:not(.highlight), #element3:not(.highlight), #element4:not(.highlight), #element5:not(.highlight)").length) {

  $("#newstyle").css("visibility", "visible");

}
<div id="element1" class="highlight"></div>
<div id="element2" class="highlight"></div>
<div id="element3"></div>
<div id="element4" class="highlight"></div>
<div id="element5" class="highlight"></div>
<div id="newstyle" style="visibility: hidden">I'm visible!</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

If it's all the same class, as in your example, we can be a bit more concise:

if (!$("#element1, #element2, #element3, #element4, #element5").not(".highlight").length) {

  $("#newstyle").css("visibility", "visible");

}

Example where they all have the class:

if (!$("#element1, #element2, #element3, #element4, #element5").not(".highlight").length) {

  $("#newstyle").css("visibility", "visible");

}
<div id="element1" class="highlight"></div>
<div id="element2" class="highlight"></div>
<div id="element3" class="highlight"></div>
<div id="element4" class="highlight"></div>
<div id="element5" class="highlight"></div>
<div id="newstyle" style="visibility: hidden">I'm visible!</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

And where at least one doesn't:

if (!$("#element1, #element2, #element3, #element4, #element5").not(".highlight").length) {

  $("#newstyle").css("visibility", "visible");

}
<div id="element1" class="highlight"></div>
<div id="element2" class="highlight"></div>
<div id="element3"></div>
<div id="element4" class="highlight"></div>
<div id="element5" class="highlight"></div>
<div id="newstyle" style="visibility: hidden">I'm visible!</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
like image 145
T.J. Crowder Avatar answered Oct 12 '22 06:10

T.J. Crowder


You can use attribute begins with selector "[id^=element]" passed to Array.prototype.every(), return $(el).is(".highlight") from .every() callback

// Note `$("#element1, #element2, #element3, #element4, #element5")`
// can be substituted for `$("[id^=element]")` here
if ([].every.call($("[id^=element]"), el => $(el).is(".highlight"))) {
  // do stuff
  console.log("ok");
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="element1" class="highlight"></div>
<div id="element2" class="highlight"></div>
<div id="element3" class="highlight"></div>
<div id="element4" class="highlight"></div>
<div id="element5" class="highlight"></div>

Alternatively you can check if element which has .id beginning with "element" and where .className contains "highlight" followed by whitespace or end of value .length is equal to .length of elements having .id beginning with "element"

if ($("[id^=element][class~=highlight]").length === $("[id^=element]").length) {
  console.log("ok")
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="element1" class="highlight"></div>
<div id="element2" class="highlight"></div>
<div id="element3" class="highlight"></div>
<div id="element4" class="highlight"></div>
<div id="element5" class="highlight"></div>
like image 28
guest271314 Avatar answered Oct 12 '22 05:10

guest271314