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.
Multiple HTML elements can share the same class.
The class attribute assigns a class name to the element, and that name can be used on many elements within the page.
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.
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.
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>
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>
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