Tiago
Tiago

Reputation: 13

Verify if certain divs contains a specific class

In my project there are 10 divs with the class .content like this:

<div class="content">Example</div>

I have already wroted a function that will atribute an .active class to my divs and they will appear like this:

<div class="content active">Example</div>

Now i need a function than will verify if all my divs with the class .content have the class .active too.

Thank you.

Upvotes: 0

Views: 54

Answers (3)

georgesjeandenis
georgesjeandenis

Reputation: 118

Pass the elements to check in the following function :

function verify(elementX, nameOfStyleToCheck) {
  return elementX.classList.contains(nameOfStyleToCheck);
}

Upvotes: 0

D. Seah
D. Seah

Reputation: 4592

you can get the list of div and check

var contentDivs = document.getElementsByClassName("content")
for (var i = 0; i < contentDivs.length; i++) {
  var div = contentDivs[i];
  if (div.classList.contains("active")) {
    // do something 
  } else {
    // do something
  }
}

Upvotes: 1

DedaDev
DedaDev

Reputation: 5249

document.querySelectorAll(".content").forEach(e => {
  console.log(e.classList.contains("active"));
});
  <div class="content active">Example</div>
  <div class="content">Example</div>
  <div class="content active">Example</div>
  <div class="content">Example</div>
  <div class="content active">Example</div>

Here is an example.

Upvotes: 0

Related Questions