craig
craig

Reputation: 581

If ALL instances of a class

I currently have the following javascript

if ( $("#checksquare1").hasClass("checksquareON") && 
$("#checksquare2").hasClass("checksquareON")  &&
$("#checksquare3").hasClass("checksquareON")  &&     
$("#checksquare4").hasClass("checksquareON")  && 
$("#checksquare5").hasClass("checksquareON")  && 
$("#checksquare6").hasClass("checksquareON") ) {.....}

However each of the #checksquare have a class of .checksquare

Is there a way of asking if ALL elements with class .checksquare have something

for example. (Obviously this is nonsense, but maybe something similar)

$(".checksquare").all.hasClass("checksquareON"){...}

Upvotes: 0

Views: 49

Answers (2)

Guffa
Guffa

Reputation: 700650

You can check if getting the elements with the added class gives the same amount of elements:

if ($('.checksquare').length == $('.checksquare.checksquareON').length) ...

Upvotes: 1

Arun P Johny
Arun P Johny

Reputation: 388416

You can check is there any checksquare which does not have checksquareON using .not()

var $checks = $(".checksquare");
if($checks.not('.checksquareON').length == 0){
}

or another way to do the same is to check whether the number of elements having checksquare and having checksquare and checksquareON are the same

var $checks = $(".checksquare");
if($checks.filter('.checksquareON').length == $checks.length){
}

Upvotes: 4

Related Questions