Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

querySelectorAll selects all similar classes

Calendar is using 3 different classes to style its child elements: "old day", "day", "new day". Trying to querySelectorAll element with class name "day" also captures the other two classes, so when i say something like:

t = document.getElementByTagName('table');
d = t.item(0).querySelectorAll('.day');
/* also selects td.new.day and td.old.day */

for (i = 0; i < d.length: i ++) {
     if(d[i].textContent == 28) {
         d[i].click();
     }
}

I will get click on old 28th instead of current month 28th.

How do i select "day" class of td element without also selecting "old day" and "new day"?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can use :not to specify which classes you don’t want to match.

document.querySelectorAll(".red:not(.big):not(.small)").forEach(e => {
  e.style.marginLeft = "100px";
});
.red {color: red;}
.big {font-size: 20px}
.small {font-size: 12px}
<a class="red">red</a><br>
<a class="red big">red big</a><br>
<a class="red small">red small</a>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading