How to find out if there is a class?

Let me add a class const box = document.querySelector('.box'); box.classList.add('active');
I want to add another class if there is an active class.
How to check if a class is active?
box.classList.contains("active")
Tried that but didn’t work.

Can you be more specific about what didn’t work?

I can’t find the class, here’s more specific

Do you have your code in codepen or github or something like that? It would probably help if we could see all of your code instead of just this tiny little bit of it.

No, no github this code on I just want to make a condition, if there is an active class, add some other class

Well, then I don’t know how to help you. The code:

box.classList.contains("active")

will return either true or false assuming that box is an HTML Element. If that is not working the way you want it to then there is something else going on and I can’t know what that is unless I can see all of your code.

Help with this js code, I need to close the form when clicking outside the element

Generally, you would add a click handler on the body and then close the form if the click was on an element that was not inside the form.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.