Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

check if an element does not contain a class in JavaScript [duplicate]

Tags:

javascript

Hello friends I have this code that works well for me to check if my element contains a certain class, but I want to achieve the opposite, that it checks me when this class does not contain the element.

if (document.querySelector(".header").classList.contains("no-sticky")) {
      alert();
    }
like image 356
mike jet Avatar asked Oct 19 '25 09:10

mike jet


1 Answers

Just put exclamation mark in front of the condition.

if (!document.querySelector(".header").classList.contains("no-sticky")) {
      alert();
    }```
like image 190
İlker Avatar answered Oct 20 '25 22:10

İlker