Categories
JavaScript Answers

How to select element that does not have specific class with JavaScript?

Spread the love

Sometimes, we want to select element that does not have specific class with JavaScript.

In this article, we’ll look at how to select element that does not have specific class with JavaScript.

How to select element that does not have specific class with JavaScript?

To select element that does not have specific class with JavaScript, we can use the document.querySelector method with the :not pseudoclass.

For instance, we write

const li = document.querySelector("li:not(.completed)");

to select the first li element that doesn’t have the completed class.

We can have multiple :not pseudoclasses in the same selector.

For instance, we write

const li = document.querySelector("li:not(.completed):not(.selected)");

to select the first li element that doesn’t have the completed or the selected class.

Conclusion

To select element that does not have specific class with JavaScript, we can use the document.querySelector method with the :not pseudoclass.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *